Create nodeJS Typescript project - slavxyz/Documentation GitHub Wiki
Welcome to the nodeJS wiki!
- Create directory
- mkdir project
- cd project
- npm initialization
- npm init -y
- Install dependency
- npm install typescript ts-node @types/node --save-dev
- Create tsconfig.json
- npx tsc --init
{
"compilerOptions": {
"target": "ES2020",
"module": "CommonJS",
"rootDir": "src",
"outDir": "dist",
"strict": true,
"esModuleInterop": true
},
"include": ["src"]
}
"compilerOptions" This section tells the TypeScript compiler (tsc) how to compile your code.
"target": "ES2020" Sets the JavaScript version that TypeScript should compile to. ES2020 enables modern JS features like optional chaining, nullish coalescing, and more. Output JS will be compatible with environments that support ES2020.
"module": "CommonJS" Specifies the module system to use in the compiled output. CommonJS is used by Node.js (require()/module.exports). Suitable for Node.js backends.
"rootDir": "src" Tells TypeScript where your TypeScript source files live. All your .ts files should be inside the src/ folder. 🔹 "outDir": "dist" Tells TypeScript to put all compiled .js files in the dist/ folder.
This separates source code (src) from output code (dist).
"strict": true Enables all strict type-checking options. Improves safety, catches bugs earlier. Includes options like noImplicitAny, strictNullChecks, etc.
"esModuleInterop": true Allows default imports from modules that use CommonJS.
Lets you do: import express from 'express';
Instead of: import * as express from 'express';
"include": ["src"] Tells TypeScript to only compile files inside the src/ folder. Prevents tsc from trying to compile unnecessary files (e.g., node_modules or configs).
- Create directory of basic stricture
- mkdir src
- Create main file
- touch src/index.ts or touch src/main.ts
- Create package.json and add following lines:
Optional: Add run scripts in package.json
"scripts": {
"start": "ts-node src/index.ts"
}
example of package.json
{
"name": "chain-of-responsibility",
"type": "module",
"scripts": {
"start": "node dist/main.js"
},
"devDependencies": {
"ts-node": "^10.9.2",
"typescript": "^5.8.3"
},
"version": "1.0.0",
"main": "index.js",
"dependencies": {
"acorn": "^8.15.0",
"acorn-walk": "^8.3.4",
"arg": "^4.1.3",
"create-require": "^1.1.1",
"diff": "^4.0.2",
"make-error": "^1.3.6",
"undici-types": "^7.8.0",
"v8-compile-cache-lib": "^3.0.1",
"yn": "^3.1.1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": ""
}
After all is ready, then compile it(parse to js):
*npx tsc
*node dist/index.js