{"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":103,"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        const cachedData = codeCache?.get(filename, cjsModule)\n        const script = new vm.Script(cjsModule, {\n          filename,\n          cachedData,\n          importModuleDynamically: options.importModuleDynamically,","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/vitest/src/runtime/vm/commonjs-executor.ts#L85-L121","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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)."],"exampleFix":"// before\nconst { register } = require('module')\nregister('./my-loader.mjs', import.meta.url)\n\n// after (in tests)\n// remove the register call; let Vitest handle module loading","handlingStrategy":"validation","validationCode":"// Guard against calling Module.register under Vitest\nconst shouldRegister = !process.env.VITEST && !globalThis.__vitest_worker__\nif (shouldRegister) {\n  require('module').register('./loader.mjs', import.meta.url)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["vm","module-system","cjs","loader"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}