{"record":{"id":"651745b4e4279e72","repo":"mastra-ai/mastra","slug":"failed-to-run-install-command-with-swpm-and-native","errorCode":null,"errorMessage":"Failed to run install command with swpm and native package managers","messagePattern":"Failed to run install command with swpm and native package managers","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/agent-builder/src/utils.ts","lineNumber":205,"sourceCode":"      } else if (packageManager === 'npm') {\n        args.push('--yes'); // npm install --yes\n\n        // Check if we're in a workspace subfolder\n        if (inWorkspace) {\n          args.push('--ignore-workspaces');\n        }\n      }\n    }\n    args.push(...packageNames);\n\n    console.info(`Falling back to ${packageManager} ${args.join(' ')}`);\n    await spawn(packageManager, args, { cwd });\n    return;\n  } catch (e) {\n    console.warn(`Failed to run install command with native package manager: ${e}`);\n  }\n\n  throw new Error(`Failed to run install command with swpm and native package managers`);\n}\n\n// Utility functions\nexport function kindWeight(kind: UnitKind): number {\n  const idx = UNIT_KINDS.indexOf(kind as any);\n  return idx === -1 ? UNIT_KINDS.length : idx;\n}\n\n// Utility functions to work with Mastra templates\nexport async function fetchMastraTemplates(): Promise<\n  Array<{\n    slug: string;\n    title: string;\n    description: string;\n    githubUrl: string;\n    tags: string[];\n    agents: string[];\n    workflows: string[];","sourceCodeStart":187,"sourceCodeEnd":223,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/agent-builder/src/utils.ts#L187-L223","documentation":"spawnSWPM attempts the swpm-managed install first and falls back to the detected native package manager (npm/pnpm/yarn/bun). If BOTH invocations fail — spawn throws on both attempts — it throws 'Failed to run install command with swpm and native package managers'. Each failure is warned to console, so the underlying causes are in the preceding warnings.","triggerScenarios":"installPackages/upgradePackages/installStep calling spawnSWPM in a directory where the swpm spawn fails (non-zero exit, missing binary) AND the fallback native package manager spawn also fails (binary not installed, bad cwd, network/auth failure on install).","commonSituations":"Scaffolding a project on a machine without npm/pnpm on PATH; invalid cwd (targetPath doesn't exist); private registry auth failures; offline environments; corrupted lockfiles making every install attempt exit non-zero.","solutions":["Read the two preceding console.warn lines to see the native error, then fix that root cause (missing binary, bad path, auth)","Install the native package manager on PATH (e.g. `npm i -g pnpm`) or point PATH at it","Verify the cwd passed to spawnSWPM exists and is a valid package directory (has package.json or can be initialized)","Retry the install after fixing network/registry access; check registry URL and tokens in .npmrc","If swpm itself is broken, ensure it is installed/up to date so the primary attempt can succeed"],"exampleFix":"// before\nawait spawnSWPM('pnpm', ['add', 'zod'], nonExistentDir);\n\n// after\nimport { existsSync } from 'node:fs';\nif (!existsSync(targetDir)) throw new Error(`Target dir ${targetDir} does not exist`);\nawait spawnSWPM('pnpm', ['add', 'zod'], targetDir);","handlingStrategy":"try-catch","validationCode":"import { existsSync } from 'node:fs';\nimport { execFileSync } from 'node:child_process';\nif (!existsSync(cwd)) throw new Error(`cwd does not exist: ${cwd}`);\nexecFileSync('npm', ['--version']); // ensures a native PM is on PATH","typeGuard":null,"tryCatchPattern":"try {\n  await installPackages(packages, cwd);\n} catch (e) {\n  if (e instanceof Error && e.message.includes('swpm and native package managers')) {\n    // inspect the earlier console.warn output for the native spawn failure and fix PATH/cwd/registry\n  } else throw e;\n}","preventionTips":["Ensure npm/pnpm/yarn/bun is installed and on PATH in the environment running the builder","Validate the target directory exists and contains package.json before installing","Check registry/auth configuration (.npmrc) in CI and containers","Capture the per-attempt console.warn output for root-cause diagnosis"],"tags":["package-manager","process-spawn","install"],"backgroundTag":"package-install-failed","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}