{"record":{"id":"f16da82ed8f6ccc7","repo":"withfig/autocomplete","slug":"npm-prefix-command-failed","errorCode":null,"errorMessage":"npm prefix command failed","messagePattern":"npm prefix command failed","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/yarn.ts","lineNumber":11,"sourceCode":"import { npmScriptsGenerator, npmSearchGenerator } from \"./npm\";\n\nexport const yarnScriptParserDirectives: Fig.Arg[\"parserDirectives\"] = {\n  alias: async (token, executeShellCommand) => {\n    const npmPrefix = await executeShellCommand({\n      command: \"npm\",\n      // eslint-disable-next-line @withfig/fig-linter/no-useless-arrays\n      args: [\"prefix\"],\n    });\n    if (npmPrefix.status !== 0) {\n      throw new Error(\"npm prefix command failed\");\n    }\n    const packageJson = await executeShellCommand({\n      command: \"cat\",\n      // eslint-disable-next-line @withfig/fig-linter/no-useless-arrays\n      args: [`${npmPrefix.stdout.trim()}/package.json`],\n    });\n    const script: string = JSON.parse(packageJson.stdout).scripts?.[token];\n    if (!script) {\n      throw new Error(`Script not found: '${token}'`);\n    }\n    return script;\n  },\n};\n\nexport const nodeClis = new Set([\n  \"vue\",\n  \"vite\",\n  \"nuxt\",","sourceCodeStart":1,"sourceCodeEnd":29,"githubUrl":"https://github.com/withfig/autocomplete/blob/aef52acff84c45edde61ae610cc2c964802b9a38/src/yarn.ts#L1-L29","documentation":"A yarn completion spec shells out to `npm prefix` to locate the global npm prefix directory, then reads package.json from it. When `npm prefix` exits non-zero, the spec throws this Error because the package.json path cannot be determined.","triggerScenarios":"`npm prefix` exits with a non-zero status: npm is not installed or not on PATH in the execution environment, npm's config/cache is corrupted, or the environment (e.g. sandboxed spec execution) blocks npm from running.","commonSituations":"Node/npm not installed while only yarn is present; nvm-managed npm unavailable in the non-interactive shell that runs the spec; broken npm installation or unreadable .npmrc causing npm to exit with an error; permission issues on the npm cache.","solutions":["Run `npm prefix` manually; if it fails, fix npm (reinstall via nvm or the Node installer)","Ensure npm is on PATH for the shell running the completion spec (`which npm`)","Repair a corrupted npm cache/config (`npm cache verify`, inspect ~/.npmrc)","If npm is intentionally absent, this error is expected — install Node.js/npm or use a yarn-specific resolution instead"],"exampleFix":"// before\nconst npmPrefix = await executeShellCommand({ command: \"npm\", args: [\"prefix\"] });\n// after (fall back to yarn global dir when npm is unavailable)\nconst prefix = npmPrefix.status === 0\n  ? npmPrefix.stdout.trim()\n  : (await executeShellCommand({ command: \"yarn\", args: [\"global\", \"dir\"] })).stdout.trim();","handlingStrategy":"fallback","validationCode":"import { execSync } from \"child_process\";\nexport function npmAvailable(): boolean {\n  try {\n    execSync(\"npm prefix\", { stdio: \"ignore\" });\n    return true;\n  } catch {\n    return false;\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  const script = await yarnScriptResolver(token);\n} catch (err) {\n  if (err instanceof Error && err.message === \"npm prefix command failed\") {\n    // npm unavailable: fall back to `yarn global dir` or skip script completion\n    return useYarnGlobalDirFallback(token);\n  }\n  throw err;\n}","preventionTips":["Ensure Node.js/npm is installed and on PATH even when primarily using yarn","Test completion specs in the same shell environment users run (PATH via nvm/volta can differ)","Run `npm prefix` as a smoke test after installing/upgrading toolchains","Check ~/.npmrc validity — a broken config makes every npm command exit non-zero"],"tags":["npm","yarn","cli","environment","path"],"backgroundTag":"required-command-not-found","analyzedSha":"aef52acff84c45edde61ae610cc2c964802b9a38","analyzedAt":"2026-08-31T11:00:43.303Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}