{"id":"f490825ac0a62b4e","repo":"vitest-dev/vitest","slug":"failed-to-create-vitest-api-token-at-tokenpaths","errorCode":null,"errorMessage":"Failed to create Vitest API token at ${tokenPaths.join(' or ')}","messagePattern":"Failed to create Vitest API token at (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/node/config/apiToken.ts","lineNumber":56,"sourceCode":"  }\n  catch {}\n  return { token, tokenCreated: true }\n}\n\nexport function resolveApiToken(root: string): { token: string; tokenCreated: boolean; tokenPath: string } {\n  const tokenPaths = [\n    join(getUserDataDir(), 'vitest', API_TOKEN_FILE),\n    join(searchForWorkspaceRoot(root), 'node_modules/.vitest', API_TOKEN_FILE),\n  ]\n\n  for (const tokenPath of tokenPaths) {\n    try {\n      return { ...resolveTokenFromPath(tokenPath), tokenPath }\n    }\n    catch {}\n  }\n\n  throw new Error(`Failed to create Vitest API token at ${tokenPaths.join(' or ')}`)\n}\n","sourceCodeStart":38,"sourceCodeEnd":58,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/vitest/src/node/config/apiToken.ts#L38-L58","documentation":"Thrown by resolveApiToken() when it fails to create or read the Vitest API token at both candidate locations: the per-user data dir (XDG_DATA_HOME/Library/Application Support/LOCALAPPDATA) and `<workspace>/node_modules/.vitest`. The token secures communication between the Vitest server and its API clients; if neither location is writable/readable, the API cannot start securely.","triggerScenarios":"Both token paths are unwritable: mkdirSync/writeFileSync throw inside resolveTokenFromPath for the user-data dir AND the node_modules/.vitest dir (e.g. read-only filesystem, permission denied, no node_modules). The outer loop swallows each error and, after exhausting both, throws.","commonSituations":"Running in a read-only container/filesystem (some CI sandboxes); HOME/XDG_DATA_HOME unset and home dir unwritable; node_modules absent and the workspace root not writable; restrictive podman/docker SELinux policies; npm install with `--no-save` leaving no writable node_modules.","solutions":["Ensure the user data dir is writable: set XDG_DATA_HOME to a writable path, or fix permissions on ~/.local/share (Linux), ~/Library/Application Support (macOS), LOCALAPPDATA (Windows).","Ensure node_modules exists and is writable inside the workspace root (run `pnpm install` / `npm install`).","In read-only CI, mount a writable tmpfs at the data dir or node_modules/.vitest.","Avoid running Vitest as a user without write access to either location."],"exampleFix":"# before: read-only fs, both paths fail\n# after (CI): set a writable data dir\nexport XDG_DATA_HOME=/tmp/vitest-data\nvitest run","handlingStrategy":"validation","validationCode":"import { access } from 'node:fs/promises'\nasync function assertWritableDir(dir: string) {\n  try { await access(dir, await import('node:fs/promises').then(() => undefined)) }\n  catch { /* fall through */ }\n  const { mkdir } = await import('node:fs/promises')\n  await mkdir(dir, { recursive: true, mode: 0o700 })\n}","typeGuard":null,"tryCatchPattern":"try {\n  resolveApiToken(root)\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith('Failed to create Vitest API token')) {\n    process.env.XDG_DATA_HOME = '/tmp/vitest-data'\n    resolveApiToken(root)\n  } else throw e\n}","preventionTips":["Ensure the user data dir (XDG_DATA_HOME / ~/Library/Application Support / LOCALAPPDATA) is writable.","Keep node_modules present and writable in the workspace root.","In read-only CI, mount a writable tmpfs at the data dir."],"tags":[],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}