June 04, 2021
backendtsnode

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
.

  1. Initialize package.json by
    1yarn init -y
  2. Add TypeScript as a Developer Dependency
    1yarn add -D typescript
  3. Now, initialize tsconfig.json file.
    1npx tsc --init

    Here, tsc refers to TypeScript compiler For testing.
  4. Create src/index.ts and put in a console.log();
  5. 1yarn add -D ts-node-dev
  6. add script in package.json:
    1"start": "ts-node-dev --respawn src/index.ts"

    Alternatively
  7. 1yarn add -g nodemon
  8. add script in package.json:
    1"start": "nodemon --exec src/index.ts"
  9. To add typescript definition:
    1yarn add -D @types/packageName