---
title: "How do I run TypeScript directly in Node.js?"
description: "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."
url: https://www.danny.engineering/answer/nodejs/how-do-i-run-typescript-directly-in-nodejs
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:24:00.000Z
tags: ["Node.js"]
---

# How do I run TypeScript directly in Node.js?

## 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.


```bash
node --version   # v22.18.0 or later
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:


```typescript
export const Status = {
  Active: 'active',
  Disabled: 'disabled',
} as const;

export type Status = (typeof Status)[keyof typeof Status];
```


And declare class fields instead of using parameter properties:


```typescript
class User {
  name: string;

  constructor(name: string) {
    this.name = name;
  }
}
```


### 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.


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


### 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.


```typescript
import { format } from './format.ts';
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.


```bash
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.

## Related reading

- [Node.js: Modules, TypeScript](https://nodejs.org/api/typescript.html)
- [TypeScript: erasableSyntaxOnly](https://www.typescriptlang.org/tsconfig/#erasableSyntaxOnly)
