Node.js CLI
Preload tsx to run TypeScript with the node command. This registers tsx before your entry file and its static imports load.
Install tsx locally and check the ESM registration requirements. Run the examples from that project directory. See scope and lifetime for the limits shared by these integrations.
Preload options
| Node.js option | Package specifier | Effect |
|---|---|---|
--import | tsx | Register both loading paths before the entry file runs |
--require | tsx/cjs | Register the CommonJS loading path |
--import | tsx/esm | Register the ESM loading path |
These options are opt-in; invoking node without a tsx preload does not register tsx. The entry file is a path passed after the preload option. Node.js controls its own flags and exit status; see the Node.js CLI reference.
hello.ts
const message: string = 'Hello from TypeScript'
console.log(message)node --import tsx ./hello.tsIt prints:
Hello from TypeScriptCustom tsconfig.json path
TSX_TSCONFIG_PATH is an optional string environment variable. When unset, tsx uses its normal tsconfig lookup. A custom path must identify an existing file; relative paths use the working directory.
For the hello.ts example, create:
tsconfig.custom.json
{}Then run from the same project directory in a POSIX shell:
TSX_TSCONFIG_PATH=./tsconfig.custom.json node --import tsx ./hello.tsIt prints:
Hello from TypeScriptIn PowerShell, set $env:TSX_TSCONFIG_PATH = './tsconfig.custom.json' before running the Node.js command.
Binaries
NODE_OPTIONS is Node.js's optional string environment variable for startup options. In a POSIX shell, this command template supplies the preload to a tool that launches Node.js:
NODE_OPTIONS='--import tsx' npx some-binaryReplace some-binary with the tool's package name. The tool must run in Node.js and be able to resolve the locally installed tsx package. Its output depends on the selected tool; successful startup lets it load TypeScript through tsx. Child Node.js processes that inherit NODE_OPTIONS also receive the setting.
Advanced usage
CommonJS mode only
Create hello.cts with the same contents as hello.ts above, then run:
node --require tsx/cjs ./hello.ctsIt prints:
Hello from TypeScriptThis selects the CommonJS loading path; see module formats for execution constraints.
Module mode only
Create hello.mts with the same contents as hello.ts above, then run:
node --import tsx/esm ./hello.mtsIt prints:
Hello from TypeScriptUse the default --import tsx integration when an application needs both loading paths.
