ESM Register API
Register tsx for dynamic import() and control when that registration stops. Use tsImport() if you only need to load a file and do not need a registration handle.
Install tsx and check ESM registration compatibility. This API uses the ECMAScript module (ESM) loading path; see scope and lifetime for shared loading and cleanup limits.
Arguments and return values
Import register from tsx/esm/api. register(options?) accepts an optional object:
| Option | Type | Default | Effect |
|---|---|---|---|
namespace | string | Omitted: global registration | A unique, nonempty string selects scoped registration |
tsconfig | string | false | Normal lookup, including TSX_TSCONFIG_PATH | Select an existing configuration file, or disable lookup with false |
onImport | (url: string) => void | Omitted: no callback | Observe URLs reported by this loader |
Relative tsconfig paths use the working directory. Without a namespace, the return value is unregister: () => Promise<void>. With a namespace, it is the same function with these properties:
| Member | Arguments | Return value |
|---|---|---|
api.import(specifier, parent) | Required specifier: string, required parent: string | Promise resolving to the imported module's exports |
api.unregister() | None | Promise<void> resolving when deactivation finishes |
The parent is the importing file's URL string or absolute path. Pass import.meta.url from ESM or __filename from CommonJS. Relative specifiers resolve from that file. See module formats for export interoperability.
Usage
Create these files in a project with tsx installed:
file.mts
export const message: string = 'Hello from TypeScript'load.mjs
import { register } from 'tsx/esm/api'
const unregister = register()
try {
const loaded = await import('./file.mts')
console.log(loaded.message)
} finally {
await unregister()
}Run from that project directory:
node ./load.mjsIt prints:
Hello from TypeScriptUse dynamic imports after register(); see entry-point evaluation order for why static imports cannot wait for runtime registration.
Scoped registration
Replace load.mjs with:
load.mjs
import crypto from 'node:crypto'
import { register } from 'tsx/esm/api'
const api = register({ namespace: crypto.randomUUID() })
try {
const loaded = await api.import('./file.mts', import.meta.url)
const loadedAgain = await api.import('./file.mts', import.meta.url)
console.log(loaded.message)
console.log(loaded === loadedAgain)
} finally {
await api.unregister()
}node ./load.mjsIt prints:
Hello from TypeScript
trueCaching and cleanup
Imports of the same resolved module within one namespace share cached module instances. A different namespace gives tsx-loaded files separate cache identities. Global registration uses the usual import cache. Call and await the returned unregister function, or api.unregister(), to wait for deactivation.
Options
To use a custom configuration, replace the register() call in the first load.mjs example with this fragment:
const unregister = register({ tsconfig: './tsconfig.custom.json' })Create this file in the project directory:
tsconfig.custom.json
{}Run node ./load.mjs; expected output remains:
Hello from TypeScriptTo disable lookup instead, use register({ tsconfig: false }) at the same call site, with the same command and expected output.
Tracking loaded files
Replace the register() call in the first load.mjs example with this fragment:
const unregister = register({
onImport: (url) => {
console.log(url)
}
})Run node ./load.mjs. In addition to Hello from TypeScript, the output includes the loaded file's URL, with a project-dependent path. Expected URL excerpt:
file:///…/file.mtsNotifications can identify dependencies as well as the entry module, including non-file specifiers such as node: URLs. Filter and convert file URLs before using them as filesystem paths. The callback reports loader activity, not every cache hit or every dependency loaded through other mechanisms. With asynchronous Node.js hooks, notifications can arrive after an import resolves.
