Run TypeScript Directly in Node.js with the New Experimental Flag
Node.js v22.6.0 introduces the experimental --experimental-strip-types flag, enabling developers to execute TypeScript files directly, but it only strips type annotations at runtime and imposes several usage constraints such as mandatory type‑only imports and explicit file extensions.
Node.js v22.6.0 introduces the experimental flag --experimental-strip-types, which enables direct execution of TypeScript files by stripping type annotations at runtime without performing type checking.
Usage
Upgrade Node.js to v22.6.0.
Create a TypeScript entry file, e.g., app.ts.
Run the file with the experimental flag:
node --experimental-strip-types app.tsType‑only Import Requirement
Because the runtime strips types, imports that bring in only types must be prefixed with the type keyword. Omitting type causes Node.js to treat the import as a value import, leading to a runtime error.
Example
// user.ts
export type User = {
name: string;
};
export function getUser(id: string) {
const user: User = { name: 'hhh' };
return user;
} // app.ts
import type { User } from "./user.ts";
import { getUser } from "./user.ts";
const user: User = getUser('1');
console.log(user);Current Limitations
Only inline type annotations are stripped; enums, namespaces, and similar constructs are not supported.
All type‑only imports must use the type keyword.
The tsconfig.json file is ignored.
Import statements must include the file extension (e.g., ./user.ts).
TypeScript handling is disabled by default inside node_modules.
References
https://nodejs.org/docs/latest/api/typescript.html
https://nodejs.org/en/blog/release/v22.6.0
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Full-Stack Cultivation Path
Focused on sharing practical tech content about TypeScript, Vue 3, front-end architecture, and source code analysis.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
