Skip to content

Entry-point ​

Register tsx from a JavaScript entry file when you cannot change the Node.js launch command. Prefer a visible preload such as node --import tsx when you control the command.

Install tsx and check ESM registration compatibility. Run the examples from the project directory. Runtime registration cannot transform the entry file itself.

Registration imports ​

Package specifierLoading pathRegistration lifetime
tsxCommonJS and ESMProcess
tsx/cjsCommonJSProcess
tsx/esmESMProcess

These imports accept no options and register by side effect; they do not return a cleanup handle. Without one of these imports or another registration mechanism, this entry-point integration is inactive. Use the ESM and CommonJS registration APIs for explicit cleanup. See scope and lifetime for shared limitations.

file.mts

ts
export const message: string = 'Hello from TypeScript'

start.mjs

js
import 'tsx'

const loaded = await import('./file.mts')
console.log(loaded.message)
sh
node ./start.mjs

It prints:

text
Hello from TypeScript

Use a dynamic import() after registration. Static imports are evaluated before the entry file's body, so adding import './file.mts' beside import 'tsx' is not a substitute for preloading tsx. See Node.js runtime hook registration.

Advanced usage ​

These alternatives select a loading path; they do not change the loaded file's module format.

CommonJS mode only ​

Create file.cts with the same export as file.mts above, then create:

start.cjs

js
require('tsx/cjs')

const loaded = require('./file.cts')

console.log(loaded.message)
sh
node ./start.cjs

It prints:

text
Hello from TypeScript

Module mode only ​

Replace start.mjs with the following file to register the ESM loader without the separate global CommonJS registration:

start.mjs

js
import 'tsx/esm'

const loaded = await import('./file.mts')
console.log(loaded.message)
sh
node ./start.mjs

It prints:

text
Hello from TypeScript