Run a TypeScript file with a single command line
NicolasBrondinBernard
Learn how to run a TypeScript file instantly with ts-node. A single command line is enough to run your code without a prior compilation step.

Article published on 13/11/2023, last updated on 10/08/2026
You may have discovered this in our introduction to TypeScript, but running .ts files can turn out to be slightly tedious (if you're not using pre-configured tools), because you need to compile the TypeScript code into JavaScript code after every change, before you can then run it…
But if you want to run server-side TypeScript, it's much simpler!
In a NodeJS environment, all you need is one very simple command to run your file, like this:
npx ts-node index.ts
What's behind the command
Let's take a look together at the tools used by this command, which runs your TypeScript code in a very transparent way (and a bit of magic).
npx
Even if you're not familiar with the npx command, you definitely know NPM, the package manager that comes with NodeJS!
Well,
npxis a command installed by default with NPM (>5.2.0)
It allows you to run the command from any NPM package, and installs all the necessary dependencies (if they aren't already installed).
Here's what happens:
- We pass the ts-node command (and the index.ts parameter) to npx
- npx checks whether the command's binary is present in the local node_modules folder AND the system's global node_modules
- If the command can't be found, npx will install the necessary dependencies
- Once the dependencies are installed, npx runs the command
This utility, in addition to simplifying the execution process, also helps work around certain issues with adding paths to the PATH variable (particularly on Windows)
ts-node
This module is a runtime engine that performs JIT (just-in-time) transpilation from TypeScript to JavaScript, which allows you to run .ts files "directly" in NodeJS, without pre-compilation!
The advantage of this solution is that it doesn't only work for your scripts, but ts-node will transpile "on the fly" all modules imported via require or import.
It's even possible to simply run
Complete courses, exercises and certificates to really learn programming!
4.8 average rating
No comments yet