CommonJS Register API
Register tsx for synchronous require() calls and control when that registration stops. If you only need an enhanced require() function, use tsx.require() instead.
Install tsx and check compatibility. This API uses the CommonJS loading path; see scope and lifetime for shared loading and cleanup limits.
Arguments and return values
Import register from tsx/cjs/api. register(options?) accepts an optional object:
| Option | Type | Default | Effect |
|---|---|---|---|
namespace | string | Omitted: global registration | A unique, nonempty string selects scoped registration |
Without a namespace, the return value is unregister: () => void. With a namespace, it is the same function with these properties:
| Member | Arguments | Return value |
|---|---|---|
api.require(id, fromFile) | Required id: string, required fromFile: string | URL | Module exports, synchronously |
api.resolve(id, fromFile, options?) | Same required arguments; optional options: { paths?: string[] } | Resolved cache key as a string, without loading |
api.unregister() | None | void |
fromFile is the importing file's absolute path, file URL string, or URL. Relative requests resolve from that file. options.paths is omitted by default and supplies Node.js's optional package search paths.
The ESM-imported registration API shares the temporary resolver limitation.
Usage
Create these files in a project with tsx installed:
file.cts
export const message: string = 'Hello from TypeScript'load.cjs
const { register } = require('tsx/cjs/api')
const unregister = register()
try {
const loaded = require('./file.cts')
console.log(loaded.message)
} finally {
unregister()
}Run from that project directory:
node ./load.cjsIt prints:
Hello from TypeScriptScoped registration
Replace load.cjs with:
load.cjs
const crypto = require('node:crypto')
const { register } = require('tsx/cjs/api')
const api = register({ namespace: crypto.randomUUID() })
try {
const loaded = api.require('./file.cts', __filename)
const loadedAgain = api.require('./file.cts', __filename)
console.log(loaded.message)
console.log(loaded === loadedAgain)
} finally {
api.unregister()
}node ./load.cjsIt prints:
Hello from TypeScript
trueCaching and cleanup
Repeated loads within one namespace share CommonJS cache entries. A different namespace gives tsx-loaded files separate cache identities. Global registration uses the normal CommonJS cache. Cleanup is synchronous: call the returned function or api.unregister() when this registration is no longer needed.
Unregistering is idempotent: repeated calls do not deactivate another registration. This applies to both global and scoped registrations.
Unregistering disables that registration's resolution and transformation hooks. It does not unload modules. Cached dependencies can remain namespaced; dependencies that are not cached, including entries removed from require.cache, resolve through the remaining loaders.
Namespaced CommonJS modules expose a ?namespace= suffix in module.filename and module.path to isolate Node's caches. Use __filename and __dirname for filesystem paths.
Configuration
The only registration option is namespace. Configuration uses TSX_TSCONFIG_PATH when set, or normal tsconfig lookup otherwise. The variable is an optional string; relative paths use the working directory. This API does not accept tsconfig or onImport options.
For the load.cjs example, create:
tsconfig.custom.json
{}Run from the same project directory in a POSIX shell:
TSX_TSCONFIG_PATH=./tsconfig.custom.json node ./load.cjsWith the scoped example, expected output is:
Hello from TypeScript
trueFor dependency inspection, see tsx.require() cache tracking.
