Brady Holt

CLI apps in TypeScript via ts-node

Mar 9, 2018

I really like the ts-node project. It allows you to run TypeScript in Node.js directly, without having to run the files through the TypeScript compiler (tsc), first. It does this by registering a handler for files with the .ts or .tsx file extentions and relying upon ts-node to marshall back and forth to the TypeScript compiler, on the fly. It also supports a TypeScript REPL which is really neat.

I’ve mainly been using it for unit tests in Node projects (example) to avoid having an intermediate build step. Also, increasingly, I’ve been using it to execute TypeScript directly on some of my own server-side projects at runtime. Again, doing this reduces a build-time step and reduces friction.

Lately, I’ve been using Node more and more to write CLI apps and thought it would be neat if ts-node could be used for this as well. It turns out it’s pretty easy.