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

  1. Remove or guard the `module.register(...)` call in test paths; Vitest already handles TS/source maps.
  2. If a third-party dependency calls register, mock or stub it in tests, or avoid requiring it in the test environment.
  3. Use Vitest's built-in TypeScript/ESM handling instead of a custom loader register.
  4. 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

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


AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03). Data as JSON: /data/errors/766a0eadceaacc56.json. Report an issue: GitHub.