vitest-dev/vitest · error · Error
[vitest] "register" is not available when running in Vitest.
Error message
[vitest] "register" is not available when running in Vitest.
What it means
Thrown by Vitest's custom `Module` shim inside the vm CommonJS executor when user code calls `module.register(...)` (Node's ESM loader registration hook). Vitest replaces the real Node Module with a sandboxed one that compiles code via vm.Script; that sandbox deliberately disables register because the loader hooks it implies cannot operate inside the Vitest vm context.
Source
Thrown at packages/vitest/src/runtime/vm/commonjs-executor.ts:103
}
const _require = Module.createRequire(this.id)
requiresCache.set(this, _require)
return _require
}
static getSourceMapsSupport = () => ({
enabled: false,
nodeModules: false,
generatedCode: false,
})
static setSourceMapsSupport = () => {
// noop
}
static register = () => {
throw new Error(
`[vitest] "register" is not available when running in Vitest.`,
)
}
static registerHooks = () => {
throw new Error(
`[vitest] "registerHooks" is not available when running in Vitest.`,
)
}
_compile(code: string, filename: string) {
const cjsModule = Module.wrap(code)
const codeCache = executor.codeCache
const cachedData = codeCache?.get(filename, cjsModule)
const script = new vm.Script(cjsModule, {
filename,
cachedData,
importModuleDynamically: options.importModuleDynamically,View on GitHub (pinned to d568f8ce37)
Solutions
- Remove or guard the `module.register(...)` call in test paths; Vitest already handles TS/source maps.
- If a third-party dependency calls register, mock or stub it in tests, or avoid requiring it in the test environment.
- Use Vitest's built-in TypeScript/ESM handling instead of a custom loader register.
- Switch the pool/environment if you truly need loader hooks (they are fundamentally incompatible with the vm executor).
Example fix
// before
const { register } = require('module')
register('./my-loader.mjs', import.meta.url)
// after (in tests)
// remove the register call; let Vitest handle module loading Defensive patterns
Strategy: validation
Validate before calling
// Guard against calling Module.register under Vitest
const shouldRegister = !process.env.VITEST && !globalThis.__vitest_worker__
if (shouldRegister) {
require('module').register('./loader.mjs', import.meta.url)
} Prevention
- Do not call module.register in code loaded by tests.
- Gate Node loader APIs behind an env check.
- Let Vitest handle TS/source maps instead of registering your own loader.
When it happens
Trigger: Test code (or a dependency it requires) calls `require('module').register(...)` or `module.register(...)` while running under Vitest's vm pool / CommonJS executor. This is common with packages that auto-register a loader hook (tsx loader, ts-node ESM loader, source-map register, custom loaders).
Common situations: A dependency self-registers an ESM loader hook on import. Code copied from a Node script that calls Module.register for TS loading. Source-map-support or similar register calls.
Related errors
- [vitest] "registerHooks" is not available when running in Vi
- Cannot parse the module format of '${url}' because "module.f
- Environment "${name}" is not a valid environment. Path "${pa
- Snapshot environment module must have a default export objec
- import of '${fileUrl}' by undefined is not supported: http c
AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03).
Data as JSON: /data/errors/766a0eadceaacc56.json.
Report an issue: GitHub.