{"record":{"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":140,"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        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\n          // per-context state and can be reused by every vm context\n          try {\n            script = new vm.Script(cjsModule, {\n              filename,\n              cachedData,\n              importModuleDynamically: activeImportModuleDynamically,","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/runtime/vm/commonjs-executor.ts#L122-L158","documentation":"Thrown by the stub `Module.registerHooks` static method on the fake CommonJS Module class inside Vitest's vm executor. registerHooks is Node's companion to module.register for setting hook callbacks; Vitest deliberately disables it because it manages its own module pipeline inside workers, and surfaces an explicit error instead of a silent no-op.","triggerScenarios":"Code under test (executed through Vitest's vm/commonjs executor) calls `module.registerHooks(...)` or `Module.registerHooks(...)` — typically a library that wires up ESM loader hooks for tracing, instrumentation, or transpilation.","commonSituations":"Testing instrumentation/tracing SDKs that register hooks at import time; porting code that relied on Node's loader hook APIs into a Vitest suite; running under the default pools that use the vm CommonJS executor.","solutions":["Gate the call so it does not run under Vitest: `if (!process.env.VITEST) Module.registerHooks(...)`.","Mock or exclude the module that performs registration in your test setup.","Use Vitest-native extension points (server.mode, deps.inline, plugins) instead of Node loader hooks.","Move the registration into a separate process spawned by the test if the hook behavior must be exercised."],"exampleFix":"// before\nimport Module from 'node:module'\nModule.registerHooks({ load() { /* ... */ } })\n\n// after\nimport Module from 'node:module'\nif (!process.env.VITEST) {\n  Module.registerHooks({ load() { /* ... */ } })\n}","handlingStrategy":"validation","validationCode":"const isVitest = !!process.env.VITEST\nif (!isVitest) {\n  await import('node:module').then(m => m.registerHooks({ load() { /* ... */ } }))\n}","typeGuard":"function shouldSkipModuleRegisterHooks(): boolean {\n  return !!process.env.VITEST\n}","tryCatchPattern":"try {\n  Module.registerHooks(hooks)\n} catch (e) {\n  if (String(e?.message).includes('\"registerHooks\" is not available when running in Vitest')) {\n    // expected under Vitest\n  } else throw e\n}","preventionTips":["Guard registerHooks with `!process.env.VITEST` in instrumented libraries.","Use Vitest plugins or server.deps.inline instead of loader hooks when possible.","Run loader-dependent code in a spawned child process from tests."],"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"}