npm i -D @types/node @types/express @types/dotenv nodemon typescript ts-node
If you have .js files already created then rename all to .ts
Run below command to create tsconfig.json
npx tsc --init
And paste below configuration in tsconfig.json
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"baseUrl": "src",
"outDir": "dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}
If you dont want to configure nodemon then directly update the dev script in package.json
{
"scripts": {
"dev": "nodemon ./src/index.ts",
"build": "npx tsc"
}
}
If you are using nodemon for development then create nodemon.json in root directory and paste below configuration in it
{
"watch": ["src", ".env"],
"ext": "js,ts,json",
"ignore": ["node_modules", "dist"],
"exec": "ts-node --transpile-only ./src/index.ts"
}
If you have configured nodemon as per above configuration then only update dev script
{
"scripts": {
"dev": "nodemon --watch",
"build": "npx tsc"
}
}
Dont add "type": "module"
in package.json
Run the project using npm run dev