{ "type": "module", "source": "doc/api/typescript.md", "modules": [ { "textRaw": "Modules: TypeScript", "name": "modules:_typescript", "introduced_in": "v22.6.0", "type": "module", "meta": { "changes": [ { "version": "v26.0.0", "pr-url": "https://github.com/nodejs/node/pull/61803", "description": "Removed `--experimental-transform-types` flag." }, { "version": [ "v25.2.0", "v24.12.0" ], "pr-url": "https://github.com/nodejs/node/pull/60600", "description": "Type stripping is now stable." }, { "version": [ "v24.3.0", "v22.18.0" ], "pr-url": "https://github.com/nodejs/node/pull/58643", "description": "Type stripping no longer emits an experimental warning." }, { "version": [ "v23.6.0", "v22.18.0" ], "pr-url": "https://github.com/nodejs/node/pull/56350", "description": "Type stripping is enabled by default." }, { "version": "v22.7.0", "pr-url": "https://github.com/nodejs/node/pull/54283", "description": "Added `--experimental-transform-types` flag." } ] }, "stability": 2, "stabilityText": "Stable", "modules": [ { "textRaw": "Enabling", "name": "enabling", "type": "module", "desc": "
There are two ways to enable runtime TypeScript support in Node.js:
\nFor full support of all of TypeScript's syntax and features, including\nusing any version of TypeScript, use a third-party package.
\nFor lightweight support, you can use the built-in support for\ntype stripping.
\nTo use TypeScript with full support for all TypeScript features, including\ntsconfig.json, you can use a third-party package. These instructions use\ntsx as an example but there are many other similar libraries available.
Install the package as a development dependency using whatever package\nmanager you're using for your project. For example, with npm:
npm install --save-dev tsx\n\nThen you can run your TypeScript code via:
\nnpx tsx your-file.ts\n\nOr alternatively, you can run with node via:
node --import=tsx your-file.ts\n\nBy default Node.js will execute TypeScript files that contains only\nerasable TypeScript syntax.\nNode.js will replace TypeScript syntax with whitespace,\nand no type checking is performed.\nTo disable this feature, use the flag --no-strip-types.
Node.js ignores tsconfig.json files and therefore\nfeatures that depend on settings within tsconfig.json,\nsuch as paths or converting newer JavaScript syntax to older standards, are\nintentionally unsupported. To get full TypeScript support, see Full TypeScript support.
The type stripping feature is designed to be lightweight.\nBy intentionally not supporting syntaxes that require JavaScript code\ngeneration, and by replacing inline types with whitespace, Node.js can run\nTypeScript code without the need for source maps.
\nType stripping is compatible with most versions of TypeScript\nbut we recommend version 5.8 or newer with the following tsconfig.json settings:
{\n \"compilerOptions\": {\n \"noEmit\": true, // Optional - see note below\n \"target\": \"esnext\",\n \"module\": \"nodenext\",\n \"rewriteRelativeImportExtensions\": true,\n \"erasableSyntaxOnly\": true,\n \"verbatimModuleSyntax\": true\n }\n}\n\nUse the noEmit option if you intend to only execute *.ts files, for example\na build script. You won't need this flag if you intend to distribute *.js\nfiles.
Node.js supports both CommonJS and ES Modules syntax in TypeScript\nfiles. Node.js will not convert from one module system to another; if you want\nyour code to run as an ES module, you must use import and export syntax, and\nif you want your code to run as CommonJS you must use require and\nmodule.exports.
.ts files will have their module system determined the same way as .js\nfiles. To use import and export syntax, add \"type\": \"module\" to the\nnearest parent package.json..mts files will always be run as ES modules, similar to .mjs files..cts files will always be run as CommonJS modules, similar to .cjs files..tsx files are unsupported.As in JavaScript files, file extensions are mandatory in import statements\nand import() expressions: import './file.ts', not import './file'. Because\nof backward compatibility, file extensions are also mandatory in require()\ncalls: require('./file.ts'), not require('./file'), similar to how the\n.cjs extension is mandatory in require calls in CommonJS files.
The tsconfig.json option allowImportingTsExtensions will allow the\nTypeScript compiler tsc to type-check files with import specifiers that\ninclude the .ts extension.
Since Node.js is only removing inline types, any TypeScript features that\ninvolve replacing TypeScript syntax with new JavaScript syntax will error.
\nThe most prominent features that require transformation are:
\nEnum declarationsnamespace with runtime codenamespaces that do not contain runtime code are supported.\nThis example will work correctly:
// This namespace is exporting a type\nnamespace TypeOnly {\n export type A = string;\n}\n\nThis will result in ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX error:
// This namespace is exporting a value\nnamespace A {\n export let x = 1\n}\n\nSince Decorators are currently a TC39 Stage 3 proposal,\nthey are not transformed and will result in a parser error.\nNode.js does not provide polyfills and thus will not support decorators until\nthey are supported natively in JavaScript.
\nIn addition, Node.js does not read tsconfig.json files and does not support\nfeatures that depend on settings within tsconfig.json, such as paths or\nconverting newer JavaScript syntax into older standards.
Due to the nature of type stripping, the type keyword is necessary to\ncorrectly strip type imports. Without the type keyword, Node.js will treat the\nimport as a value import, which will result in a runtime error. The tsconfig\noption verbatimModuleSyntax can be used to match this behavior.
This example will work correctly:
\nimport type { Type1, Type2 } from './module.ts';\nimport { fn, type FnParams } from './fn.ts';\n\nThis will result in a runtime error:
\nimport { Type1, Type2 } from './module.ts';\nimport { fn, FnParams } from './fn.ts';\n",
"displayName": "Importing types without `type` keyword"
},
{
"textRaw": "Non-file forms of input",
"name": "non-file_forms_of_input",
"type": "module",
"desc": "Type stripping can be enabled for --eval and STDIN. The module system\nwill be determined by --input-type, as it is for JavaScript.
TypeScript syntax is unsupported in the REPL, --check, and\ninspect.
Since inline types are replaced by whitespace, source maps are unnecessary for\ncorrect line numbers in stack traces; and Node.js does not generate them.
", "displayName": "Source maps" }, { "textRaw": "Type stripping in dependencies", "name": "type_stripping_in_dependencies", "type": "module", "desc": "To discourage package authors from publishing packages written in TypeScript,\nNode.js refuses to handle TypeScript files inside folders under a node_modules\npath.
tsconfig \"paths\" won't be transformed and therefore produce an error. The closest\nfeature available is subpath imports with the limitation that they need to start\nwith #.