Skip to content

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:

OptionTypeDefaultEffect
namespacestringOmitted: global registrationA unique, nonempty string selects scoped registration
tsconfigstring | falseNormal lookup, including TSX_TSCONFIG_PATHSelect an existing configuration file, or disable lookup with false
onImport(url: string) => voidOmitted: no callbackObserve 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:

MemberArgumentsReturn value
api.import(specifier, parent)Required specifier: string, required parent: stringPromise resolving to the imported module's exports
api.unregister()NonePromise<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

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

load.mjs

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

sh
node ./load.mjs

It prints:

text
Hello from TypeScript

Use 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

js
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()
}
sh
node ./load.mjs

It prints:

text
Hello from TypeScript
true

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

js
const unregister = register({ tsconfig: './tsconfig.custom.json' })

Create this file in the project directory:

tsconfig.custom.json

json
{}

Run node ./load.mjs; expected output remains:

text
Hello from TypeScript

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

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

text
file:///…/file.mts

Notifications 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.