{"record":{"id":"997b3d8774597772","repo":"vitest-dev/vitest","slug":"failed-to-load-custom-defines-error-message-997b3d","errorCode":null,"errorMessage":"Failed to load custom \"defines\": ${error.message}","messagePattern":"Failed to load custom \"defines\": (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/runtime/workers/vm.ts","lineNumber":158,"sourceCode":"      context,\n      externalModulesExecutor,\n    },\n    configurable: true,\n    enumerable: false,\n    writable: false,\n  })\n  context.__vitest_mocker__ = moduleRunner.mocker\n\n  setupEnv(ctx.config.env, state.metaEnv)\n\n  if (ctx.config.serializedDefines) {\n    try {\n      runInContext(ctx.config.serializedDefines, context, {\n        filename: 'virtual:load-defines.js',\n      })\n    }\n    catch (error: any) {\n      throw new Error(`Failed to load custom \"defines\": ${error.message}`)\n    }\n  }\n  await moduleRunner.mocker.initializeSpyModule()\n\n  const { run } = (await moduleRunner.import(\n    entryFile,\n  )) as typeof import('../runVmTests')\n\n  try {\n    await run(\n      method,\n      ctx.files,\n      ctx.config,\n      moduleRunner,\n      traces,\n    )\n  }\n  finally {","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/runtime/workers/vm.ts#L140-L176","documentation":"Same as [367] but on the vm worker path: Vitest runs serializedDefines via runInContext inside the vm context. If the generated JS fails, the inner error is wrapped as 'Failed to load custom \"defines\": <message>'. Injects config.define values as globals into the vm sandbox before tests run.","triggerScenarios":"A define value in vitest.config that is not valid as the body evaluated inside the vm context (functions, circular objects, BigInt, Symbols), or values whose serialized form throws when evaluated. Caught and re-thrown with the inner message.","commonSituations":"Non-JSON values in define when running the vm pool; a Vite/Vitest plugin injecting non-serializable defines; environment-specific globals that conflict with define names.","solutions":["Keep all define values JSON-serializable.","Inspect the inner error.message (appended after the colon) to find the offending key.","Move runtime values into an imported module rather than define.","Test the serializedDefines string in isolation to reproduce the parse/eval error."],"exampleFix":"// before\ndefine: { __cfg: someClassInstance }\n\n// after\ndefine: { __cfg: JSON.stringify({ mode: 'test' }) }","handlingStrategy":"validation","validationCode":"function validateDefines(obj) {\n  for (const [k, v] of Object.entries(obj)) {\n    try { new Function(`return (${JSON.stringify(v)})`)() } catch {\n      throw new Error(`define '${k}' fails to evaluate in a vm context`)\n    }\n  }\n}","typeGuard":"function isVmSafeDefine(v) {\n  try { new Function(`return (${JSON.stringify(v)})`)(); return true } catch { return false }\n}","tryCatchPattern":null,"preventionTips":["Restrict define to JSON-safe primitives and plain objects.","Inspect the inner wrapped message to find the offending key.","Prefer importing runtime values over inlining them in define."],"tags":["config","define","vm","serialization"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}