ssm0801 / Express-with-Typescript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Express + Typescript

Install required packages

npm i -D @types/node @types/express @types/dotenv nodemon typescript ts-node

Change files extension

If you have .js files already created then rename all to .ts

Configure Typescript

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
  }
}

Update package.json

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"
  }
}

Nodemon configuration [optional]

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"
}

Update package.json

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

Final

Run the project using npm run dev

About


Languages

Language:TypeScript 100.0%