More Node.js answers

by Danny
Published 2 min read

Modern Node can run .ts files with no build step by stripping the types. Learn which versions do it by default, what syntax it cannot handle, and how to set up tsconfig for it.

The short answer

#

On Node 22.18.0 or later, or 23.6.0 or later, you can run a TypeScript file directly. Node removes the type annotations and runs what is left, with no compiler and no extra packages. That covers the current LTS lines, Node 24 and Node 26.


_10
node --version # v22.18.0 or later
_10
node src/index.ts

Node does not type check your code, so keep running tsc --noEmit in your editor or CI.

Why it happens

#

This feature is called type stripping. Node reads the .ts file, replaces type annotations, interfaces and other type-only syntax with whitespace, and runs the remaining JavaScript. Because it only removes code and never generates any, it is fast and line numbers in stack traces stay correct without source maps.

It first arrived behind the --experimental-strip-types flag, and was turned on by default in 23.6.0 and then in 22.18.0. Earlier releases print an experimental warning when it runs.

The catch is that some TypeScript features are not just types. They produce JavaScript, so stripping alone cannot handle them:

  • enum declarations
  • namespace blocks that contain runtime code
  • Parameter properties such as constructor(private name: string)
  • import x = require('x') style imports

Node throws a syntax error when it meets these.

How to fix it

#

Write erasable TypeScript

The simplest approach is to avoid the features above. Replace an enum with a plain object and a type:


_10
export const Status = {
_10
Active: 'active',
_10
Disabled: 'disabled',
_10
} as const;
_10
_10
export type Status = (typeof Status)[keyof typeof Status];

And declare class fields instead of using parameter properties:


_10
class User {
_10
name: string;
_10
_10
constructor(name: string) {
_10
this.name = name;
_10
}
_10
}

Set up tsconfig.json to match

These options make the compiler warn you about anything Node cannot run. erasableSyntaxOnly needs TypeScript 5.8 or later.


_10
{
_10
"compilerOptions": {
_10
"target": "esnext",
_10
"module": "nodenext",
_10
"noEmit": true,
_10
"allowImportingTsExtensions": true,
_10
"erasableSyntaxOnly": true,
_10
"verbatimModuleSyntax": true
_10
}
_10
}

Use .ts extensions and import type

Node resolves files the same way it does for JavaScript, so relative imports need the real file extension. Type-only imports must be marked with type, so Node knows to remove them.


_10
import { format } from './format.ts';
_10
import type { Config } from './config.ts';

Whether a .ts file is treated as an ES module or CommonJS follows the same type field rule as .js. Use .mts or .cts to force one or the other.

Turn on transforms for enums

If you cannot remove enums or parameter properties yet, Node has a flag that generates the missing JavaScript. It is still experimental.


_10
node --experimental-transform-types src/index.ts

Use a runner when you need more

Type stripping does not read tsconfig.json, so paths aliases, decorators and JSX are not supported. If you depend on those, a tool such as tsx still works well, or compile with tsc and run the output.

Common pitfalls

#
  • Expecting type errors to stop the program. Node never checks types.
  • Importing ./format or ./format.js when the file is format.ts.
  • Leaving off import type for an interface, which makes Node try to import a value that does not exist.
  • Publishing raw .ts files in a package. Node will not strip types inside node_modules, so ship compiled JavaScript.
  • Trying it on Node 20. That line never had type stripping and is end of life; upgrade to an LTS release.
#

Was this resource helpful?