Run TypeScript in Node.js without a build step

Authors
  • avatar
    Name
    Hamza Rahman
Published on
-
9 mins read

An up-to-date install of any supported Node.js release line can run a TypeScript file directly with node file.ts, with no flags or extra packages. Below is what works, what throws, the tsconfig.json settings that catch unsupported syntax early, and a worked ts-node migration.

Checked on December 4, 2026 with Node.js 22.23.5, 24.23.0, 25.2.1 and 26.14.0, and TypeScript 7.0.2.

Run a file

// hello.ts
interface User {
  name: string
}

function greet(user: User): string {
  return `Hi ${user.name}`
}

console.log(greet({ name: 'Ada' }))
node hello.ts
# Hi Ada

Node replaces the type annotations with whitespace and runs the JavaScript that is left, a process called type stripping. Because the remaining code stays on its original lines and columns, stack traces line up without source maps.

Node does not check the types. This file runs happily:

const total: number = 'ten'
console.log(total) // ten

Which Node.js versions support it

The release schedule lists Node 26 as Active LTS since October 28, 2026. Node 24 and 22 are in Maintenance LTS. Node 25 is end of life, and Node 27 is still in alpha.

Release lineStatusType strippingFlag needed--experimental-transform-types
26Active LTSStableNoRemoved in 26.0.0
25End of lifeStable since 25.2.0NoExperimental
24Maintenance LTSStable since 24.12.0NoExperimental
22Maintenance LTSRelease candidateNo, since 22.18.0Experimental

The status and version numbers come from the Node 26, Node 24 and Node 22 docs.

Check that last column before upgrading to Node 26. Earlier releases can compile enums and parameter properties with --experimental-transform-types, but Node 26 removed the flag:

node --experimental-transform-types app.ts
# node: bad option: --experimental-transform-types

If a script depends on that flag, rewrite the syntax or use tsx before you move to Node 26.

What works and what throws

Syntax that can be erased works: annotations, interfaces, type aliases, generics, as, satisfies, non-null !, import type, and namespaces that contain only types.

Syntax that Node can't erase throws. Each row below gives the same error on Node 22, 24, 25 and 26:

SyntaxResult
enum and const enumERR_UNSUPPORTED_TYPESCRIPT_SYNTAX
namespace with runtime codeERR_UNSUPPORTED_TYPESCRIPT_SYNTAX
constructor(private x: number)ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX
import fs = require('node:fs')ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX
<string>value castsERR_UNSUPPORTED_TYPESCRIPT_SYNTAX
DecoratorsSyntaxError
.tsx filesERR_UNKNOWN_FILE_EXTENSION in ES modules, SyntaxError for JSX in CommonJS
tsconfig.json path aliasesERR_MODULE_NOT_FOUND
.ts files inside node_modulesERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING

Node 26.14.0 adds an experimental flag, --experimental-strip-private-modules, for the last row. It only covers packages marked "private": true, such as workspace packages in a monorepo.

Type stripping also works with node -e and stdin, but not with node --check, the REPL or node inspect.

Fix your imports

Three import rules catch most existing code.

Write the real extension. Node does not guess it, and it does not map .js to .ts.

import { helper } from './helper.ts' // works
// import { helper } from './helper'    ERR_MODULE_NOT_FOUND
// import { helper } from './helper.js' ERR_MODULE_NOT_FOUND

CommonJS follows the same rule: write require('./helper.ts'), not require('./helper').

Mark type imports. Without type, Node treats the name as a value that should exist at runtime.

import { helper, type Options } from './helper.ts'

If you leave the keyword off, you get does not provide an export named 'Options'.

Use imports in package.json for aliases. Node ignores paths, but it supports subpath imports, which start with #.

{
  "type": "module",
  "imports": {
    "#lib/*": "./src/lib/*.ts"
  }
}
import { helper } from '#lib/helper'

Put .ts in the mapping, as above. If you map to ./src/lib/* and import #lib/helper.ts instead, Node runs it, but tsc reports TS2877 with the settings below because it cannot rewrite a non-relative path.

Module format follows the same rules as JavaScript: "type": "module" in package.json for ES modules, or .mts and .cts to choose per file. Node never converts between the two.

Use a matching tsconfig.json

Node ignores tsconfig.json. This config is for your editor and tsc, so type-checking rejects the same syntax Node would fail on at runtime.

{
  "compilerOptions": {
    "noEmit": true,
    "target": "esnext",
    "module": "nodenext",
    "types": ["node"],
    "strict": true,
    "erasableSyntaxOnly": true,
    "verbatimModuleSyntax": true,
    "rewriteRelativeImportExtensions": true,
    "skipLibCheck": true
  },
  "include": ["src"]
}

module, target and the last three flags before skipLibCheck are the settings the Node docs recommend, for TypeScript 5.8 or newer.

  • erasableSyntaxOnly reports enums, parameter properties and the rest as TS1294.
  • verbatimModuleSyntax makes you write import type where Node needs it.
  • rewriteRelativeImportExtensions allows .ts in relative imports. If you later emit JavaScript, it rewrites them to .js.
  • types: ["node"] is needed since TypeScript 6.0, where types defaults to an empty list. Without it, tsc cannot find node:fs even when @types/node is installed.

Type-check in CI

Keep running and checking as two separate commands.

npm install -D typescript @types/node
{
  "scripts": {
    "start": "node src/report.ts",
    "test": "node --test",
    "typecheck": "tsc"
  }
}
npm run typecheck && npm test

With noEmit in the config, tsc checks types without writing files. Given the bad assignment from earlier, saved as src/bad.ts, it exits with code 1:

src/bad.ts(1,7): error TS2322: Type 'string' is not assignable to type 'number'.

The built-in test runner picks up .test.ts files with no extra configuration, on Node 22, 24, 25 and 26.

Migrate a ts-node script

Here is a small CommonJS-style script that runs with ts-node 10.9.2 and TypeScript 5.9.3. It counts errors in a log file.

// src/lib/stats.ts (before)
export enum Level {
  Info = 'info',
  Error = 'error',
}

export interface Entry {
  level: Level
  message: string
}

export class LogStats {
  constructor(private readonly entries: Entry[]) {}

  count(level: Level): number {
    return this.entries.filter((entry) => entry.level === level).length
  }
}
// src/report.ts (before)
import { readFileSync } from 'fs'
import path from 'path'
import { Entry, Level, LogStats } from './lib/stats'

const file = path.join(__dirname, '..', 'log.ndjson')
const entries: Entry[] = readFileSync(file, 'utf8')
  .trim()
  .split('\n')
  .map((line) => JSON.parse(line))

const stats = new LogStats(entries)
console.log(`errors: ${stats.count(Level.Error)}`)

TypeScript 7.0 is one reason to migrate. It ships without a programmatic API, and ts-node depends on that API. With TypeScript 7.0.2 installed, ts-node 10.9.2 crashes before it reaches your code:

TypeError: Cannot read properties of undefined (reading 'fileExists')

For what else changes with that upgrade, see migrating to the TypeScript 7 native compiler.

1. Update package.json

Replace ts-node with the current TypeScript:

npm uninstall ts-node
npm install -D typescript@latest @types/node@latest

Then set the module type, the scripts and the Node version you need:

{
  "type": "module",
  "engines": { "node": ">=24.12.0" },
  "scripts": {
    "report": "node src/report.ts",
    "typecheck": "tsc"
  }
}

The engines floor is 24.12.0 because that is where the 24 line marks type stripping as stable.

Set "type": "module" before you run tsc. Without it, the next step prints ten errors, most of them complaints about import and export statements in a CommonJS file.

2. Let tsc list the problems

Replace tsconfig.json with the one above and run npm run typecheck:

src/lib/stats.ts(2,13): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
src/lib/stats.ts(13,15): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
src/report.ts(4,40): error TS2835: Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './lib/stats.js'?

3. Remove the unsupported syntax

Replace the enum with a const object plus a type of the same name, and write the class field out in full:

// src/lib/stats.ts (after)
export const Level = {
  Info: 'info',
  Error: 'error',
} as const

export type Level = (typeof Level)[keyof typeof Level]

export interface Entry {
  level: Level
  message: string
}

export class LogStats {
  private readonly entries: Entry[]

  constructor(entries: Entry[]) {
    this.entries = entries
  }

  count(level: Level): number {
    return this.entries.filter((entry) => entry.level === level).length
  }
}

Call sites such as Level.Error do not change. Running tsc again leaves only the import error.

4. Fix the entry file

Ignore the .js suggestion in that error and write ./lib/stats.ts, because Node loads the .ts file directly. Once the path resolves, tsc reports the next problem:

src/report.ts(4,10): error TS1484: 'Entry' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.

Mark Entry with type, and tsc passes. It still misses one thing: @types/node declares __dirname globally, so nothing warns you that it doesn't exist in ES modules. Node throws ReferenceError: __dirname is not defined in ES module scope when the script runs. Use import.meta.dirname instead:

// src/report.ts (after)
import { readFileSync } from 'node:fs'
import path from 'node:path'
import { Level, LogStats, type Entry } from './lib/stats.ts'

const file = path.join(import.meta.dirname, '..', 'log.ndjson')
const entries: Entry[] = readFileSync(file, 'utf8')
  .trim()
  .split('\n')
  .map((line) => JSON.parse(line))

const stats = new LogStats(entries)
console.log(`errors: ${stats.count(Level.Error)}`)

5. Run it

npm run typecheck && npm run report
# errors: 2

The migrated version prints the same result on Node 22.23.5, 24.23.0, 25.2.1 and 26.14.0.

When you still need tsx or a bundler

Use tsx when you need to run an existing codebase without rewriting it. On tsx 4.23.15, enums, runtime namespaces, parameter properties, decorators, path aliases and extensionless imports all run. Like Node's type stripping, it skips type-checking, and it does not support emitDecoratorMetadata. Code that relies on decorator metadata needs a compiler that emits it.

Use a bundler or a compile step when:

  • You ship code to the browser or write .tsx.
  • You publish a package. Node refuses to strip types under node_modules, so publish JavaScript and .d.ts files.
  • You need to target an older runtime. Node does not down-level newer JavaScript syntax.

When to use this

Plain node file.ts fits scripts, command-line tools, internal services and tests that run on Node 24 or 26. It is also a good default for small AI scripts, such as an OpenAI structured outputs example with Zod or a tool-calling script. Keep tsc in CI and write erasable syntax, and the same files still work if you add a build later.

References