withfig/autocomplete · error · Error

npm prefix command failed

Error message

npm prefix command failed

What it means

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.

Source

Thrown at src/yarn.ts:11

import { npmScriptsGenerator, npmSearchGenerator } from "./npm";

export const yarnScriptParserDirectives: Fig.Arg["parserDirectives"] = {
  alias: async (token, executeShellCommand) => {
    const npmPrefix = await executeShellCommand({
      command: "npm",
      // eslint-disable-next-line @withfig/fig-linter/no-useless-arrays
      args: ["prefix"],
    });
    if (npmPrefix.status !== 0) {
      throw new Error("npm prefix command failed");
    }
    const packageJson = await executeShellCommand({
      command: "cat",
      // eslint-disable-next-line @withfig/fig-linter/no-useless-arrays
      args: [`${npmPrefix.stdout.trim()}/package.json`],
    });
    const script: string = JSON.parse(packageJson.stdout).scripts?.[token];
    if (!script) {
      throw new Error(`Script not found: '${token}'`);
    }
    return script;
  },
};

export const nodeClis = new Set([
  "vue",
  "vite",
  "nuxt",

View on GitHub (pinned to aef52acff8)

Solutions

  1. Run `npm prefix` manually; if it fails, fix npm (reinstall via nvm or the Node installer)
  2. Ensure npm is on PATH for the shell running the completion spec (`which npm`)
  3. Repair a corrupted npm cache/config (`npm cache verify`, inspect ~/.npmrc)
  4. If npm is intentionally absent, this error is expected — install Node.js/npm or use a yarn-specific resolution instead

Example fix

// before
const npmPrefix = await executeShellCommand({ command: "npm", args: ["prefix"] });
// after (fall back to yarn global dir when npm is unavailable)
const prefix = npmPrefix.status === 0
  ? npmPrefix.stdout.trim()
  : (await executeShellCommand({ command: "yarn", args: ["global", "dir"] })).stdout.trim();
Defensive patterns

Strategy: fallback

Validate before calling

import { execSync } from "child_process";
export function npmAvailable(): boolean {
  try {
    execSync("npm prefix", { stdio: "ignore" });
    return true;
  } catch {
    return false;
  }
}

Try / catch

try {
  const script = await yarnScriptResolver(token);
} catch (err) {
  if (err instanceof Error && err.message === "npm prefix command failed") {
    // npm unavailable: fall back to `yarn global dir` or skip script completion
    return useYarnGlobalDirFallback(token);
  }
  throw err;
}

Prevention

When it happens

Trigger: `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.

Common situations: 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.

Related errors


AI-assisted analysis of withfig/autocomplete@aef52acff8 (2026-08-31). Data as JSON: /api/errors/f16da82ed8f6ccc7. Report an issue: GitHub.