nodemon. For Typescript, we’re not quite as lucky. But we do have a few options!
In this short article, I’ll try to elaborate on some of those choices as well as my experience working with some of them.
There’s a few simple requirements that we need met for our ideal solution:
- Typescript compiles every time we save a
.tsfile in our project
node dist/index.js(or something similar).
So let’s get cracking!
First up, tsc –watch
Using typescript we automatically get this option automatically, no need to install anything extra. In the same manner we can manually compile our ts code on demand using
tsc we can tell it to watch our files using
This reads the configuration provided by our
tsconfig.json file and makes it easy to use, as we don’t need to supply anything extra. Easy!
Unfortunately, this method also has a few drawbacks.
- Ships with Typescript, so available out of the box
- Easy configuration implied from
In the end, this didn’t end up working for me, as the one con it has is a big one for me.
Let’s try ts-node
Our second contender today is called
Let’s take a look.
ts-node is easy to setup and only required me to install one extra library, assuming of course you have
typescript installed locally in your project.
You simply input the primary command the library offer – sharing the same name as the library itself – followed by the name of the file you wish to execute (let’s use index.ts as our example):
We now get the desired compilation and whenever we make an update to
index.ts, or any of the linked files in our project, yay!
It simply calls
node under the hood and collapses two simple steps into one for you.
Ready to call it a day?
… Well, unfortunately, I’m not!
As it turns out,
ts-node is quite ineffective and as such takes a long time to compile and re-render. This gets incredibly annoying faster than it should, and I was already seeing compilations taking 10-15, sometimes 30 seconds at a mid-sized project that was only halfway through its conversion from js to ts.
That won’t work for me, so let’s keep looking. But first, let’s sum up.
- Handles both ts compilation and js rendering
- Fast to get going, as it supports
tsconfig.jsonand gets its configuration from there.
NODE_OPTIONSflag(s), which can be helpful for debugging.
- Is an extra library you have to install
- Slow compilation
So let’s keep looking!
Our final entry of the day,
ts-node-dev is by far my favorite library for typescript compilation.
This library borrows a bit from
ts-node and specifically tries to deal with the issue of slow compilation that it faces.
It also supports passthrough configuration from both
ts-node is also built upon).
ts-node-dev has, in my experience, been by far the fastest compilation experience, and the more optimized we have made our codebase, the better it has gotten.
In other words, it beats
ts-node by dimensions.
Another benefit is that we have seen an enormous reduction in the amount of memory/heap overflow issues we have had, but perhaps that is just a consequence of the general typescript optimizations we’ve made over time.
- Fast compilation!
- Handles both ts compilation and js re-rendering
- Easy to configure and relies on
tsconfig.jsonas well as extra configuration, specific to the library
- Supports passthrough configuration from
- Does not support
NODE_OPTIONflag(s), which means we have to exit
nodewith the flag(s) we need.
- Is an extra library on top of
typescriptthat we have to install and manage
Conclusion: Best library for typescript compilation
All in all, I would definitely recommend trying all of these out and see if you get the same results! If you do, I’m always curious to learn and hear other experiences with libraries.
If you’re just curious about becoming a better Typescript developer in general, why not sign up for my newsletter and get tips and tricks directly in your inbox?
I mail about once a week and promise I won’t spam you 🙂