paint-brush
How to Restart the TypeScript NodeJS Application Fastby@naveenda
3,177 reads
3,177 reads

How to Restart the TypeScript NodeJS Application Fast

by NaveenSeptember 29th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

How to Restart the TypeScript NodeJS Application Fast: How to restart the Typescript NodeJS application Fast. I found a way to improve the performance and code quality of NodeJS-Typescript. I recommend to everyone use the ts-node-dev-instead of ts- node-node for your NodeJS project-Typscript project-typescript project. I have been working for the different kinds of Node JS application from the last 4 years. For every project I tried to improve. For example, I used the nodejs-typscript tool for developing typescript.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - How to Restart the TypeScript NodeJS Application Fast
Naveen HackerNoon profile picture

I have been working for the different kinds of Node JS application from the last 4 years. For every project I tried to improve the performance and code quality. From that, I found a very interesting way to restart the typescript application so fast.

Introduction

When you are working with Node JS with Typescript you notice a significant delay while restarting the application by using ts-node.

ts-node
one of the best tool for NodeJS-Typescript development but when comes to typescript it doesn't do the best job.

Typical Typescript NodeJS App

A typical nodejs-typescript

nodemon.json 
look like these

{
    "watch" : ["src"],
    "ext" : "ts",
    "exec": "ts-node ./src/app/server.ts"
}

It will work fine but restart speed is too slow.

Don't trust the Server Start time here, it actually took more than 20 seconds to restart.

Trail 1

So I was playing with the nodemon options to make restart faster and found we map our executable with params, so I create this configuration for

nodemon.json

{
  "env": {
    "NODE_ENV": "development"
  },
  "execMap": {
    "ts": "node --require ts-node/register"
  },
  "ext": "js,json,ts",
  "ignore": [
    ".git",
    "node_modules/**/node_modules"
  ],
  "restartable": "rs",
  "watch": [
    "src/"
  ]
}

This increase the restarting speed, but that not enough for me. So again I started to digging through Google to find better ways.

Trail 2

Now I learned to diagnosis typescript and analysis my project by using this command:

./node_modules/.bin/tsc -project ./tsconfig.json --diagnostics

This gives an output like these:

Files:              372
Lines:            66423
Nodes:           253384
Identifiers:      89381
Symbols:          62938
Types:             3327
Instantiations:    7640
Memory used:    108724K
I/O read:         0.98s
I/O write:        0.02s
Parse time:       2.20s
Bind time:        0.40s
Check time:       0.29s
Emit time:        0.16s
Total time:       3.04s

I found

tsc
the command takes only 3 seconds to diagnosis the entire project and the
parse time
is only taking the time. So started to find an alternatives for the ts-node.

Instead of using

ts-node
I tried to restart the application using
tsc
command.

{
"dev": "concurrently \"tsc -w\" \"nodemon dist/app/server.js\""
}

This improves the restarting speed significantly


Trail 3

tsc
command works great for me, but I still digging google due to curiosity and found a package called ts-node-dev

ts-node-dev 
works very different than ts-node. It restarts the target node process only when the files changes and also provide the same file for typescript compilation process between restarts.

This significantly increases the speed of restarting comparing to 

nodemon -x ts-node
, etc...

Step 1: Install the package

npm i ts-node-dev --save-dev

Step 2: Add Configuration

{
    "dev": "node_modules/.bin/ts-node-dev --respawn --notify false --prefer-ts --ignore-watch node_modules -- src/app/server.ts "

}

This is the exact configuration I used in my project, this works very fine to me.

Step 3: Starts the application

npm run dev

This will restart the application every time you change a file with blazing speed.

Conclusion

After testing a lot of tool for developing typescript-nodejs I found the ts-node-dev work like a pro. I recommend to everyone to use the

ts-node-dev
instead of ts-node for your NodeJS-Typescript project.

If you stick this far, thank you so much for reading this article and please consider to follow me on twitter