{"record":{"id":"d1fd2f5610b45344","repo":"vercel/next.js","slug":"failed-to-find-package-manager","errorCode":null,"errorMessage":"Failed to find package manager","messagePattern":"Failed to find package manager","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/next-codemod/lib/handle-package.ts","lineNumber":106,"sourceCode":"        case 'package-lock.json':\n          return 'npm'\n        default:\n          return 'npm'\n      }\n    }\n    // No lock file found, default to npm\n    return 'npm'\n  } catch {\n    return 'npm'\n  }\n}\n\nexport function uninstallPackage(\n  packageToUninstall: string,\n  pkgManager?: PackageManager\n) {\n  pkgManager ??= getPkgManager(process.cwd())\n  if (!pkgManager) throw new Error('Failed to find package manager')\n\n  let command = 'uninstall'\n  if (pkgManager === 'yarn') {\n    command = 'remove'\n  }\n\n  try {\n    execa.sync(pkgManager, [command, packageToUninstall], {\n      stdio: 'inherit',\n      shell: true,\n    })\n  } catch (error) {\n    throw new Error(\n      `Failed to uninstall \"${packageToUninstall}\". Please uninstall it manually.`,\n      { cause: error }\n    )\n  }\n}","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/vercel/next.js/blob/0ae8c72462952df163f1b1e0726641bc5b40dc93/packages/next-codemod/lib/handle-package.ts#L88-L124","documentation":"`handle-package.ts`'s `uninstallPackage` defaults the manager via `getPkgManager(process.cwd())`; if that returns a falsy value it throws this. Note `getPkgManager` itself has a `catch { return 'npm' }` fallback, so in practice it almost never returns falsy — this throw guards the case where a caller passes an explicit `undefined`/`null` *and* the lookup returns undefined. The same guard exists in `installPackages`. It signals the codemod could not determine which package manager to drive.","triggerScenarios":"A codemod that uninstalls/installs a package calls `uninstallPackage(name)` (or `installPackages`) when `getPkgManager` somehow returned undefined — e.g. an exceptional environment where both `npm_config_user_agent` is unset and `find-up` for every lockfile threw, bypassing the catch fallback, or a caller passed `packageManager: undefined` explicitly while cwd is unreadable.","commonSituations":"Custom codemod invoking these helpers from an unusual cwd; test mocks that stub getPkgManager to return undefined; file-system permission errors preventing lockfile discovery.","solutions":["Run the codemod from a directory that contains (or is under) a lockfile (package-lock.json, pnpm-lock.yaml, yarn.lock, bun.lock/bun.lockb).","Pass an explicit `packageManager` argument to `uninstallPackage`/`installPackages` so the lookup is skipped.","Ensure `npm_config_user_agent` is set (running under npm/pnpm/yarn/bun normally sets it).","Check read permissions on the project directory and lockfiles."],"exampleFix":"// before\nuninstallPackage('old-pkg') // throws if lookup returns undefined\n\n// after — pass manager explicitly\nimport { getPkgManager } from '@next/codemod/lib/handle-package'\nuninstallPackage('old-pkg', getPkgManager(process.cwd()) ?? 'npm')","handlingStrategy":"validation","validationCode":"import { getPkgManager } from '@next/codemod/lib/handle-package'\nconst pm = getPkgManager(process.cwd()) ?? 'npm'\nuninstallPackage('old-pkg', pm)","typeGuard":"function isPackageManager(pm: unknown): pm is 'npm'|'pnpm'|'yarn'|'bun' {\n  return typeof pm === 'string' && ['npm','pnpm','yarn','bun'].includes(pm)\n}","tryCatchPattern":"try {\n  uninstallPackage(pkg)\n} catch (e) {\n  if (/Failed to find package manager/.test(e.message)) {\n    uninstallPackage(pkg, 'npm') // explicit fallback\n  } else throw e\n}","preventionTips":["Pass an explicit packageManager to uninstall/install helpers.","Run codemods from a directory containing a lockfile.","Ensure npm_config_user_agent is set by invoking through a package manager."],"tags":["next-codemod","package-manager","lockfile","environment","defensive"],"analyzedSha":"0ae8c72462952df163f1b1e0726641bc5b40dc93","analyzedAt":"2026-08-06T19:44:29.143Z","schemaVersion":2},"datasetVersion":"2026-08-07T02:17:10.218Z"}