{"record":{"id":"766a0eadceaacc56","repo":"vitest-dev/vitest","slug":"vitest-register-is-not-available-when-running","errorCode":null,"errorMessage":"[vitest] \"register\" is not available when running in Vitest.","messagePattern":"\\[vitest\\] \"register\" is not available when running in Vitest\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/runtime/vm/commonjs-executor.ts","lineNumber":134,"sourceCode":"        }\n\n        const _require = Module.createRequire(this.id)\n        requiresCache.set(this, _require)\n        return _require\n      }\n\n      static getSourceMapsSupport = () => ({\n        enabled: false,\n        nodeModules: false,\n        generatedCode: false,\n      })\n\n      static setSourceMapsSupport = () => {\n        // noop\n      }\n\n      static register = () => {\n        throw new Error(\n          `[vitest] \"register\" is not available when running in Vitest.`,\n        )\n      }\n\n      static registerHooks = () => {\n        throw new Error(\n          `[vitest] \"registerHooks\" is not available when running in Vitest.`,\n        )\n      }\n\n      _compile(code: string, filename: string) {\n        const cjsModule = Module.wrap(code)\n        const codeCache = executor.codeCache\n        let script = cjsScriptCache.get(filename)\n        if (!script) {\n          const cachedData = codeCache?.get(filename, cjsModule)\n          // the dynamic import callback is a static function (the executor is\n          // resolved when it is called), so the compiled script holds no","sourceCodeStart":116,"sourceCodeEnd":152,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/runtime/vm/commonjs-executor.ts#L116-L152","documentation":"Thrown by the stub `Module.register` static method on the fake CommonJS Module class used inside Vitest's vm executor (commonjs-executor.ts). Node's real `module.register()` registers an ESM loader hook chain; Vitest provides its own module system inside workers, so that API is intentionally disabled and surfaces this explicit error rather than silently doing nothing.","triggerScenarios":"Code under test (loaded via Vitest's vm/commonjs executor) calls `module.register(...)` or `Module.register(...)` — e.g. a library that self-registers a custom ESM loader, or application code calling the module register API at import time.","commonSituations":"Testing a package that uses `module.register` to hook imports (e.g. for instrumentation, tracing, or tsx-style loading); running such code under Vitest's default forks/threads pools which use the vm-based CommonJS executor.","solutions":["Avoid calling `module.register` in code that runs under Vitest; gate it behind an environment check (e.g. only register when not in a Vitest worker).","Use Vitest's own loader/server.mode or `server.deps.inline` mechanisms instead of a self-registering loader.","Mock or stub the module that calls `register` in your test setup if its loader behavior is not under test.","If the registration is essential, run that code in a child process spawned from the test rather than inside the Vitest module system."],"exampleFix":"// before\nimport Module from 'node:module'\nModule.register('./my-loader.mjs')\n\n// after — skip registration under Vitest\nimport Module from 'node:module'\nif (!process.env.VITEST) {\n  Module.register('./my-loader.mjs')\n}","handlingStrategy":"validation","validationCode":"// Skip module.register when running under Vitest\nconst isVitest = !!process.env.VITEST\nif (!isVitest) {\n  await import('node:module').then(m => m.register('./loader.mjs'))\n}","typeGuard":"function shouldSkipModuleRegister(): boolean {\n  return !!process.env.VITEST\n}","tryCatchPattern":"try {\n  Module.register('./loader.mjs')\n} catch (e) {\n  if (String(e?.message).includes('\"register\" is not available when running in Vitest')) {\n    // expected under Vitest; fall through\n  } else throw e\n}","preventionTips":["Gate Node loader-API calls behind a `!process.env.VITEST` check in libraries you test.","Prefer Vitest-native extension points over self-registering loaders.","Mock modules that register loaders when their hook behavior is not under test."],"tags":["vm","module-system","esm-loader","runtime"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}