Lately I have started to combine Typescript more and more with nodejs coding. In this post I will show how to incorporate Typescript into a nodemon based workflow.
Why bother with Typescript in Node?
However, I am already doing all my Angular development in Typescript. Adding Typescript to the server code feels like a natural extension.
Not only does it give me a consistent coding pattern, but it also opens the door to sharing types and interfaces between client and server.
I am used to using nodemon to refresh my node app every time I make a change to the source.
How does Typescript compilation integrate with nodemon?
Luckily this is pretty easy since nodemon allows you to execute custom tasks for every change.
The base command I use is:
This tells nodemon to rerun the –exec command every time there is a file change.
In my case I am using the ngc compiler since I am doing Angular AoT compilation. However, for plain Typescript you would substitute ngc for tsc.
To simplify the nodemon command, I’ve moved the watch configurations to a separate nodemon.json file:
This tells nodemon which folders and file extensions to watch. There is also a command to force a restart (typing rs in the console will restart).
Depending on your application you may also need a tsconfig file to guide the Typescript compiler.
My sample file can be found below: