Running TypeScript in Node Without a Build Step
Node 22.18 strips types by default now, so `node script.ts` just works. What I moved over, what still needs a compiler, and the one tsconfig flag that keeps it honest.
For years, running a TypeScript file in Node meant picking a tool first: ts-node, then tsx, sometimes a compile step into dist. At the end of July, Node 22.18 shipped with type stripping on by default, the same behavior that landed in Node 23.6 at the start of the year. On an LTS line, that's the version I can actually use at work.
node scripts/seed.tsNo loader, no flag, no build.
What Node actually does
Node doesn't compile TypeScript. It erases it. Type annotations get replaced with whitespace, and the rest runs as JavaScript. Because nothing moves, line numbers in stack traces still match the source.
That design explains every limit.
What works, and what doesn't
| Works | Needs a real compiler |
|---|---|
| Type annotations, interfaces, generics | enum |
import type and satisfies | Namespaces with runtime code |
| Plain classes | Constructor parameter properties |
ES modules with .ts extensions | Path aliases from tsconfig paths |
Node also doesn't read tsconfig.json and doesn't type-check. It runs what's there.
The flag that keeps it honest
TypeScript 5.8 added erasableSyntaxOnly. Turn it on and the compiler errors on anything Node can't strip, so you find out in the editor instead of at runtime:
{
"compilerOptions": {
"erasableSyntaxOnly": true,
"verbatimModuleSyntax": true,
"allowImportingTsExtensions": true,
"noEmit": true
}
}Enums were the main thing I had to replace. A const object with as const does the same job and is plain JavaScript:
export const OrderStatus = {
Pending: "pending",
Paid: "paid",
Shipped: "shipped",
} as const;
export type OrderStatus = (typeof OrderStatus)[keyof typeof OrderStatus];Where I use it
- Scripts. Seeds, migrations, one-off data fixes. This is the big win, because these never deserved a build setup.
- Small services. A webhook receiver or a cron worker runs straight from source.
- Not the main apps. Frameworks already have their own pipeline, and type-checking still runs in CI with
tsc --noEmit.
Why it matters
Each tool between writing code and running it is something to install, configure, and debug when it breaks. Type stripping removes one of them for the code where it was pure overhead. The type checker still does its job. It just stopped being in the way of running the file.