remix/node-tsx
Run Node.js with TypeScript and JSX syntax support in .ts, .tsx, and .jsx files.
node-tsx transforms supported source files before Node.js executes them, including
TypeScript syntax that requires JavaScript code generation such as enums, runtime
namespaces, and parameter properties. JSX compiler options are read from the nearest
tsconfig.json for each loaded file.
The loader does not type check, change Node.js module resolution, apply TypeScript path aliases, or downlevel JavaScript syntax for older runtimes.
Installation
npm i remixUsage
Pass the --import flag to register the remix/node-tsx loader when executing node:
node --import remix/node-tsx ./server.tsTypeScript configuration
Since import resolution still follows Node.js, configure type checking to match native Node loading:
{
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext",
"allowImportingTsExtensions": true,
"isolatedModules": true,
"verbatimModuleSyntax": true,
"rewriteRelativeImportExtensions": true
}
}moduleandmoduleResolutionuseNodeNextso TypeScript resolves modules the way Node does.allowImportingTsExtensionsallows source files to import.tsand.tsxmodules directly.isolatedModulesavoids patterns that require whole-program compilation, such as re-exporting a type withoutexport type.verbatimModuleSyntaxrequires type-only imports and exports to be marked so runtime imports are unambiguous.rewriteRelativeImportExtensionspreserves atscemit path by rewriting relative.tsand.tsximports to JavaScript extensions.
Do not enable erasableSyntaxOnly if you want TypeScript to accept the same
transform-only syntax that node-tsx can execute.
Programmatic usage
Registering the loader
Import remix/node-tsx as a side effect to register the loader.
import 'remix/node-tsx'Loading a module with scoped JSX support
Load a module with TypeScript and JSX syntax support scoped to its import graph:
import { loadModule } from 'remix/node-tsx/load-module'
let mod = await loadModule('./app/server.tsx', import.meta.url)Related Work
License
See LICENSE