- Create TypeScript config file:
tsc --init
- Create package.json file:
yarn init -y
- Dependencies:
yarn add express
- Dev dependencies:
yarn add -D @types/express ts-node-dev tsconfig-paths typescript
- @types/express : Express package type. (In dependencies, we use express for application, so we need to install type of express.)
- ts-node-dev : Directly execute TypeScript on Node.js without precompiling. (Also use in scripts in package.json like nodemon to recompile when files change.)
- tsconfig-paths : Required if you config paths in tsconfig.json and want to use ts-node-dev
- Go to package.json
- Create dev script:
"ts-node-dev --respawn --transpile-only --watch src -r tsconfig-paths/register src/index.ts"
- To run application (in terminal) using command:
yarn dev
- --respawn : Keep watching for change after executed.
- --watch src : Hot reload all files in src.
- -r tsconfig-paths/register : Require tsconfig.json and use this config file.
- src/index.ts : Last arguments is main file to compile.