Skip to content

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 optionPackage specifierEffect
--importtsxRegister both loading paths before the entry file runs
--requiretsx/cjsRegister the CommonJS loading path
--importtsx/esmRegister 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

ts
const message: string = 'Hello from TypeScript'
console.log(message)
sh
node --import tsx ./hello.ts

It prints:

text
Hello from TypeScript

Custom 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

json
{}

Then run from the same project directory in a POSIX shell:

sh
TSX_TSCONFIG_PATH=./tsconfig.custom.json node --import tsx ./hello.ts

It prints:

text
Hello from TypeScript

In 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:

sh
NODE_OPTIONS='--import tsx' npx some-binary

Replace 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:

sh
node --require tsx/cjs ./hello.cts

It prints:

text
Hello from TypeScript

This 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:

sh
node --import tsx/esm ./hello.mts

It prints:

text
Hello from TypeScript

Use the default --import tsx integration when an application needs both loading paths.