docs/dev-api/register-cjs.md
The CommonJS Register API allows you to manually register the enhancement at runtime. But note, this only affects CommonJS modules (.cjs/.cts, and .js/.ts when package.json#type is unset or commonjs).
::: warning Caveats
import() calls in the loaded files are not enhanced because they're handled by Node's ESM hook. Use with the ESM Register API.const tsx = require('tsx/cjs/api')
// Register tsx enhancement
const unregister = tsx.register()
const loaded = require('./file.ts')
// Unregister when needed
unregister()
If you want to register without affecting the entire runtime environment, you can add a namespace.
When a namespace is provided, it will return a private require() method for you to load files with:
const tsx = require('tsx/cjs/api')
const api = tsx.register({
// Pass in a unique namespace
namespace: Date.now().toString()
})
// Pass in the request and the current file path
const loaded = api.require('./file.ts', __filename)
api.unregister()