To set up a Node.js project with TypeScript, follow the steps below:
Run the following command to initialize a new Node.js project with a default package.json file:
npm init -y
The npm init -y command initializes the project without interactive prompts, automatically accepting default values for the package.json file.
To install TypeScript and the type definitions for Node.js, use the following command:
npm install -D typescript @types/node
The command npm install -D typescript @types/node is used to install TypeScript and the TypeScript type definitions for Node.js as dev dependencies in your Node.js project.
Update the package.json file to include a build script and specify the module type as "module". Open the package.json file and modify it as follows:
{
"name": "typescript",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"build": "tsc"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/node": "^20.3.1",
"typescript": "^5.1.3"
}
}
"type": "module": This line sets the module type to module. This indicates that your code will be using ES modules instead of CommonJS modules. "scripts": { "build": "tsc" }: This adds a build script named build that executes the tsc command. tsc is the TypeScript compiler, and running this script will compile your TypeScript code into JavaScript.
Create a tsconfig.json file in the root directory of your project and add the following configuration:
{
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext",
"target": "ES2022",
"sourceMap": true,
"outDir": "dist"
},
"include": ["src/**/*"]
}
"compilerOptions": This section specifies the compiler options for TypeScript.
"module": "NodeNext": This option enables compatibility with ES modules and CommonJS modules in Node.js.
"moduleResolution": "NodeNext": This option tells TypeScript to use Node.js-style module resolution when resolving module imports.
"target": "ES2022": This option sets the ECMAScript target version to ES2022, which is the version of JavaScript that TypeScript will compile your code to.
"sourceMap": true: This option generates source map files alongside the compiled JavaScript code. Source maps help with debugging by mapping the compiled code back to the original TypeScript code.
"outDir": "dist": This option specifies the output directory for the compiled JavaScript files.
"include": ["src/**/*"]: This specifies the files that should be included for compilation. In this case, it includes all files in the src directory and its subdirectories.