June 04, 2021
If you're using
TypeScript with Node.js
Getting started with TypeScript with Node.
I use
1yarn as my package manager so the following code says the same.If you're using
1npm, just replace 1yarn add with 1npm install.
- Initialize package.json by
1yarn init -y - Add TypeScript as a Developer Dependency
1yarn add -D typescript - Now, initialize tsconfig.json file.
1npx tsc --init
Here, tsc refers to TypeScript compiler For testing. - Create src/index.ts and put in a console.log();
1yarn add -D ts-node-dev- add script in package.json:
1"start": "ts-node-dev --respawn src/index.ts"
Alternatively 1yarn add -g nodemon- add script in package.json:
1"start": "nodemon --exec src/index.ts" - To add typescript definition:
1yarn add -D @types/packageName