How can I run TypeScript files directly from the command line?
I’m having difficulty finding an answer. In plain Node.js, you run JavaScript files using node path/to/file.js, and with CoffeeScript, it’s coffee hello.coffee. ES6 files can be run with babel-node hello.js. What is the equivalent for TypeScript?
My project includes a tsconfig.json used by Webpack/ts-loader to build a bundle for the browser. However, I need a build step to run from the console that utilizes some of the TypeScript files to generate a schema, without compiling the entire project. How can I execute a single TypeScript file without having to compile the whole project?
I’ve run into this before as well. The simplest approach is to use the TypeScript compiler (tsc
) in watch mode. You can do that with this command:*
tsc -w -p .
This will continuously compile your TypeScript files into JavaScript, which can then be executed like any regular JS file using Node.js, for example:
node foo.js
However, a better solution if you don’t want to deal with manual compilation is to use ts-node
, which allows you to run TypeScript files on the fly without the need to precompile. It’s as easy as running:
npx ts-node src/foo.ts
This way, you can execute the TypeScript file directly without building the entire project."
Yeah, ts-node
is definitely a good option, but just to add a bit more, if you’re working with TypeScript regularly, you might want to install the necessary tools globally so you can run TypeScript files more easily. You can do this with:*
npm install -g ts-node typescript @types/node
Once installed globally, you can run a TypeScript file directly with:
ts-node typescript-file.ts
It’s also helpful because it takes care of TypeScript types, Node types, and makes the whole experience seamless when you run TypeScript files. This is really useful for those quick execution tasks without needing to compile the entire project!
I’ve been working with TypeScript for a while, and like you, I ran into some issues with ts-node
. It works well for most cases, but if you’re looking for something faster and more flexible, I’d recommend checking out @digitak/esrun
. It’s a lightweight wrapper around esbuild
, which is faster and supports both ESM and CJS imports.*
The command is simple:
esrun my-file.ts
Here are some advantages of esrun
over ts-node
:
-
Speed: Since it uses
esbuild
, it’s much faster than ts-node
.
-
Compatibility: It supports both ESM and CommonJS imports, which is great if you’re mixing import/export styles.
-
Watch Mode: Use the
--watch
option to automatically re-run the script on file changes, which is super handy for iterative development.
-
Debugging: You can use DevTools for debugging, which is a more robust option compared to the terminal console.
To install esrun
, just run:
npm install -g @digitak/esrun esbuild
Then, you can run TypeScript files directly with:
npx @digitak/esrun file.ts
In my experience, if you’re looking for speed and flexibility, this is one of the best ways to run TypeScript files efficiently without compiling your whole project!"