Since Node.js v22.7.0, there's a --experimental-transform-types flag that lets me run TypeScript files natively in Node.js without the need for extra tools like tsx or ts-node.
Info
Update (August 2025): Node.js v22.18.0 now runs TypeScript files without needing any command line flags! However, it uses type stripping (--experimental-strip-types) by default, which means some TypeScript features like class parameter properties are not available. For full TypeScript transformation support, --experimental-transform-types still needs to be used. The information below is still applicable for older versions of Node.js.
This means I can now:
- Build and run Node.js applications in TypeScript without extra build steps
- Deploy functions (like AWS Lambda or Google Cloud Run Functions) written in TypeScript without extra build steps
This is how I set it up Node.js projects:
I add this in
package.jsonto ensure Node treats the files in the project as an ES module by default:"type": "module",I install the type definitions and tsconfig preset:
pnpm add -D @types/node@22 @tsconfig/node22I use this
tsconfig.json:{ "extends": "@tsconfig/node22/tsconfig.json", "compilerOptions": { "verbatimModuleSyntax": true, "allowImportingTsExtensions": true, "noEmit": true } }This config ensures compatibility with Node 22's module system and TypeScript's latest features.
Option Why it is needed verbatimModuleSyntaxMust be set to true, otherwise, TypeScript will let you import types without thetypekeyword, which is not supported in Node 22. All non-type imports must be a value, otherwise you will get a “SyntaxError: The requested module does not provide an export named '...'” error.allowImportingTsExtensionsMust be set to true, otherwise, TypeScript will tell you to import.tsfiles with the.jsextension. But since.tsfiles are now run natively without being compiled to.js, the.jsfiles do not exist. By setting this totrue, TypeScript will let you import.tsfiles with the.tsextension.noEmitMust be set to true, otherwise TypeScript will try to compile the files to.jsfiles, which is not needed since Node can now run.tsfiles directly.To run TypeScript files, the
--experimental-transform-typesflag must be passed to Node CLI. There are many ways to do this:- By setting the
NODE_OPTIONSenvironment variable - By passing the flag directly on the CLI
In some projects, I add this to the
scriptssection of mypackage.json:"node": "node --experimental-transform-types --env-file=.env"This lets me run TypeScript files and load environment variables from a
.envfile.Now, I can just run:
pnpm node src/index.ts…and Node will execute my TypeScript code directly, with no build step or extra tooling required.
- By setting the