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 specifier | Loading path | Registration lifetime |
|---|---|---|
tsx | CommonJS and ESM | Process |
tsx/cjs | CommonJS | Process |
tsx/esm | ESM | Process |
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
export const message: string = 'Hello from TypeScript'start.mjs
import 'tsx'
const loaded = await import('./file.mts')
console.log(loaded.message)node ./start.mjsIt prints:
Hello from TypeScriptUse 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
require('tsx/cjs')
const loaded = require('./file.cts')
console.log(loaded.message)node ./start.cjsIt prints:
Hello from TypeScriptModule mode only
Replace start.mjs with the following file to register the ESM loader without the separate global CommonJS registration:
start.mjs
import 'tsx/esm'
const loaded = await import('./file.mts')
console.log(loaded.message)node ./start.mjsIt prints:
Hello from TypeScript