How to Run TypeScript code using Node.js
Since V22.6.0, Node.js has experimental support for some TypeScript syntax via "type stripping". This means that you can write code that's valid TypeScript directly in Node.js without the need to transpile it first. Let’s see an example:
If we have a simple TypeScript program hello.ts below
We can run the program below using Node.js by running this command on your terminal
Since Node v23+, --experimental-strip-types is enabled by default and therefore we can run a TypeScript program using the command node hello.ts
What really happens to the code when run by Node.js?
By default Node.js will execute TypeScript files that contain only erasable TypeScript syntax. This means that Node.js will replace TypeScript syntax with whitespace, and no type checking is performed. This means that our hello program above would be transformed to something like
Node.js ignores tsconfig.json
files and therefore features that depend on settings within tsconfig.json
, such as paths or converting newer JavaScript syntax to older standards, are intentionally unsupported.
What if you need Full TypeScript support
To use TypeScript with full support for all TypeScript features, including tsconfig.json
, you can use a third-party package. For example using the tsx library you can run the following command to install it as a dev dependency in your project.
You can then run your code with the command
or
Conclusion
TypeScript support in Node.js is still an experimental feature at this time and there may be changes along the way. Because of these limitations,there is no type checking or linting built into the runtime currently. If you prefer a fully fledged JavaScript runtime with builtin TypeScript support please check out Deno. You can learn more about Deno at https://deno.com/.