{"id":"6791000c0951880e","repo":"vitest-dev/vitest","slug":"vitest-registerhooks-is-not-available-when-run","errorCode":null,"errorMessage":"[vitest] \"registerHooks\" is not available when running in Vitest.","messagePattern":"\\[vitest\\] \"registerHooks\" 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":109,"sourceCode":"\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        const cachedData = codeCache?.get(filename, cjsModule)\n        const script = new vm.Script(cjsModule, {\n          filename,\n          cachedData,\n          importModuleDynamically: options.importModuleDynamically,\n        } as any)\n        if (cachedData && script.cachedDataRejected) {\n          codeCache!.delete(filename)\n        }\n        // @ts-expect-error mark script with current identifier\n        script.identifier = filename","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/vitest/src/runtime/vm/commonjs-executor.ts#L91-L127","documentation":"Thrown by Vitest's custom `Module` shim when user code calls `module.registerHooks(...)` (Node's runtime hooks registration, added in newer Node versions). Like register, this is intentionally disabled in the sandboxed vm CommonJS executor because runtime hooks cannot be applied to modules compiled and evaluated inside the Vitest vm context.","triggerScenarios":"Test code or a transitive dependency calls `require('module').registerHooks(...)` (or `Module.registerHooks`) while executing under Vitest's vm-based CommonJS executor. Some instrumentation/tracing/profiling libraries call registerHooks automatically.","commonSituations":"Using a profiling or tracing library that auto-registers Node runtime hooks. Copying initialization code from a server bootstrap into a test. Newer Node-version-specific code paths that hit registerHooks.","solutions":["Remove the `Module.registerHooks(...)` call from code exercised in tests.","Stub the dependency that calls registerHooks via vi.mock, or exclude it from the test bundle.","Gate the call behind an environment check (e.g. only when not running under Vitest).","Avoid Node runtime-hook APIs in code that Vitest loads."],"exampleFix":"// before\nconst Module = require('module')\nModule.registerHooks({ evaluateLoaders() { /* ... */ } })\n\n// after\nconst Module = require('module')\nif (!process.env.VITEST) {\n  Module.registerHooks({ evaluateLoaders() { /* ... */ } })\n}","handlingStrategy":"validation","validationCode":"const shouldRegisterHooks = !process.env.VITEST && !globalThis.__vitest_worker__\nif (shouldRegisterHooks) {\n  require('module').registerHooks({ /* ... */ })\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Avoid Node runtime-hook APIs in test-loaded code.","Gate registerHooks behind a non-Vitest env check.","Stub libraries that auto-register hooks via vi.mock."],"tags":["vm","module-system","cjs","loader"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}