Personal notes for understanding how TypeScript works with Node.js
Covers setup, compilation, and ideal development workflow.
Note:
TypeScript code cannot run directly. It must be compiled to JavaScript, then can be run using Node.
- mkdir typescript
- cd typescript
- npm init -y
- Creates package.json
run this command, npm i -D typescript, this will create
- node_modules/
- package-lock.json
- adds TypeScript to devDependencies
-D means devDependency (used only during development)
- Create tsconfig.json
- npx tsc --init
- π rootDir β where
.tsfiles live - π outDir β where compiled
.jsfiles go - π dist folder is auto-created, not manual
(just need to uncomment the outDir and rootDir)
- Write TypeScript Code inside src/index.ts then run
- npx tsc
- π Converts all .ts files in src/
- π Outputs .js, .d.ts, .map files in dist/
Now from here there are several ways to run ur code:-
-
- Is that you can make two scripts in package.json "build":"tsc", "start":"node dist/demo.js" or for all file *.js
In this case, Whenever u make a new ts file u need to first convert that file into js file using tsc ie npm run build and then u can simply run npm start.
-
- Second ways is to run ur ts code using nodemon
-
the catch with nodemon is that it cant read typescript or it cant compile typescript code so we need to maintain 2 terminals:-
Compile in watch mode ( 1st terminal)
- npx tsc -w
- π Keeps watching files
- π Auto-recompiles on save
- π No need to re-run command
install nodemon doing,
-
npm i -D nodemon
-
add a script:
-
"dev": "nodemon dist/*.js"
-
And then finally run this in a seperate terminal that npm run dev.
So, the final ideal flow will be:
- Add a new file in src and write ur typescript code
- then simply install nodemon
- add a dev script
- open a temrninal and run npx tsc -w ( watch mode ) this will compile ur ts file into js files and add some .map .d.ts.map files in dist folder
- then simply open another terminal and run -- npm run dev thats it.
Note:
At last tsc is used to compile ur ts code into js code and node is used to finally run ur code
Terminal 1 β TypeScript watch mode npx tsc --watch π what will this do? As soon as you will save a file with .ts extention TypeScript automatically compiles and dist/ will have .js file π That is: β dont need to run npx tsc again and again Auto compile