Full answer
How it works
- node app.ts removes TypeScript types from the code and runs the rest as JavaScript. It replaces the types with spaces, so line numbers stay the same. It is enabled by default in current Node versions.
- It is a strip, not a compile. Nothing checks types. A wrong type runs without any error.
- node:module also exports stripTypeScriptTypes(code), which is marked experimental, to see the result.
What works and what does not
- Works: type annotations, interface, type aliases, generics, import type, as casts, satisfies.
- Needs --experimental-transform-types: enum, namespace with runtime code, constructor parameter properties. These are not erasable because they produce code. Node reports ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX otherwise. Many teams avoid them (the erasableSyntaxOnly option in tsconfig enforces this).
- Imports must name the real file: import { x } from './lib.ts'. Use import type for types so the import is removed. Set verbatimModuleSyntax and allowImportingTsExtensions in tsconfig.
- tsconfig.json is not read by Node for paths or other options. Path aliases need the package.json imports field.
- Files in node_modules are not stripped, so published packages must contain JavaScript.
Typical workflow
- Run with node app.ts in development and production for simple services, or compile with tsc or a bundler for libraries and older Node versions.
- Type check separately: tsc --noEmit in CI and in the editor.
- tsx (esbuild based) runs TypeScript on any Node version, supports enums, path aliases and watch mode with tsx watch. ts-node is an older option.
- Type definitions for Node come from @types/node, matched to your Node version.
javascript
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const { spawnSync } = require('node:child_process');
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ts-'));
const write = (name, text) => fs.writeFileSync(path.join(dir, name), text);
const run = (...args) => spawnSync(process.execPath, args, { cwd: dir, encoding: 'utf8' });
const lastLine = (text) => text.trim().split('\n').filter(Boolean).pop() || '(no output)';
write('lib.ts', "export type Student = { name: string; marks: number };\nexport const grade = (s: Student): string => (s.marks >= 75 ? 'A' : 'B');\n");
write('main.ts', "import { grade } from './lib.ts';\nimport type { Student } from './lib.ts';\nconst s: Student = { name: 'Asha', marks: 78 };\nconsole.log(s.name, 'gets', grade(s));\n");
write('wrong.ts', "const n: number = 'not a number';\nconsole.log('ran anyway with', n);\n");
write('enum.ts', "enum Level { Low, High }\nconsole.log('High is', Level.High);\n");
console.log('1. type stripping :', lastLine(run('main.ts').stdout));
console.log('2. wrong types :', lastLine(run('wrong.ts').stdout), '(no type check)');
const enumRun = run('enum.ts');
console.log('3. enum :', /ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX/.test(enumRun.stderr) ? 'ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX' : lastLine(enumRun.stdout));
console.log('4. transform-types :', lastLine(run('--experimental-transform-types', '--no-warnings', 'enum.ts').stdout));
console.log('5. tsx with enum :', lastLine(spawnSync(process.execPath, [require.resolve('tsx/cli'), 'enum.ts'], { cwd: dir, encoding: 'utf8' }).stdout));
console.log('6. stripped code :', run('--no-warnings', '-p', "JSON.stringify(require('node:module').stripTypeScriptTypes('const x: number = 1;'))").stdout.trim());
fs.rmSync(dir, { recursive: true });Small TypeScript files are created and run with plain node, with the transform flag, and with tsx. The last line shows what stripping does to a line of code. Install first with npm install tsx (run here with tsx 4.23.15). Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): 1. type stripping : Asha gets A 2. wrong types : ran anyway with not a number (no type check) 3. enum : ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX 4. transform-types : High is 1 5. tsx with enum : High is 1 6. stripped code : "const x = 1;"
Key points
- Node strips types and runs the JavaScript, with no type check.
- Enums and parameter properties are not erasable.
- Imports use the .ts extension, and import type is needed for types.
- Run tsc --noEmit in CI for type safety.
- tsx helps on older Node versions and for extra features.
Common mistakes
- Thinking that node app.ts checks types.
- Using enum and being surprised by ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX.
- Importing './lib' without the .ts extension.
- Expecting tsconfig paths to work at run time.
- Publishing .ts files in a package and expecting Node to strip them in node_modules.
Follow-ups
- What does erasableSyntaxOnly check?
- When would you compile with tsc instead of running .ts directly?
- How does tsx differ from Node's type stripping?
Tips
- Say 'strip, not compile, no type check'.
- Mention enum as the main limitation.
- Say that tsc --noEmit belongs in CI.