{"record":{"id":"7afee7c4c9fbf360","repo":"yarnpkg/yarn","slug":"workspacesremoverootcheck","errorCode":null,"errorMessage":"workspacesRemoveRootCheck","messagePattern":"workspacesRemoveRootCheck","errorType":"exception","errorClass":"MessageError","httpStatus":null,"severity":"error","filePath":"src/cli/commands/remove.js","lineNumber":37,"sourceCode":"  commander.description('Removes a package from your direct dependencies updating your package.json and yarn.lock.');\n  commander.usage('remove [packages ...] [flags]');\n  commander.option('-W, --ignore-workspace-root-check', 'required to run yarn remove inside a workspace root');\n}\n\nexport function hasWrapper(commander: Object, args: Array<string>): boolean {\n  return true;\n}\n\nexport async function run(config: Config, reporter: Reporter, flags: Object, args: Array<string>): Promise<void> {\n  const isWorkspaceRoot = config.workspaceRootFolder && config.cwd === config.workspaceRootFolder;\n\n  if (!args.length) {\n    throw new MessageError(reporter.lang('tooFewArguments', 1));\n  }\n\n  // running \"yarn remove something\" in a workspace root is often a mistake\n  if (isWorkspaceRoot && !flags.ignoreWorkspaceRootCheck) {\n    throw new MessageError(reporter.lang('workspacesRemoveRootCheck'));\n  }\n\n  const totalSteps = args.length + 1;\n  let step = 0;\n\n  // load manifests\n  const lockfile = await Lockfile.fromDirectory(config.lockfileFolder);\n  const rootManifests = await config.getRootManifests();\n  const manifests = [];\n\n  for (const name of args) {\n    reporter.step(++step, totalSteps, `Removing module ${name}`, emoji.get('wastebasket'));\n\n    let found = false;\n\n    for (const registryName of Object.keys(registries)) {\n      const registry = config.registries[registryName];\n      const object = rootManifests[registryName].object;","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/yarnpkg/yarn/blob/c2dda503f3759b5be5f0e24ecd9cf5c97a540147/src/cli/commands/remove.js#L19-L55","documentation":"When `yarn remove` is invoked in a workspace root (config.cwd === config.workspaceRootFolder), removing a dependency there would affect every workspace. As a guard, remove.js:39-40 throws workspacesRemoveRootCheck unless `--ignore-workspace-root-check` (-W) is passed.","triggerScenarios":"Running `yarn remove <pkg>` while CWD is the workspace root, without the `-W` flag.","commonSituations":"Being parked in the monorepo root by default; tooling that shells out from the root; forgetting which workspace you're in.","solutions":["Move into the specific workspace package directory and run `yarn remove <pkg>` there.","If you truly intend to remove the dependency from the root workspace, pass `-W` / `--ignore-workspace-root-check`.","Confirm your CWD with `pwd` and the workspace layout with `yarn workspaces info` before retrying."],"exampleFix":"# before (in workspace root)\n$ yarn remove lodash   # workspacesRemoveRootCheck\n# after\n$ cd packages/my-app && yarn remove lodash\n# or, intentionally at root\n$ yarn remove lodash -W","handlingStrategy":"validation","validationCode":"const path = require('path');\nfunction assertNotRootUnlessAllowed(config, flags) {\n  const isRoot = config.workspaceRootFolder && config.cwd === config.workspaceRootFolder;\n  if (isRoot && !flags.ignoreWorkspaceRootCheck) {\n    throw new Error('Refusing remove in workspace root; cd into a workspace or pass -W.');\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  await runYarn(['remove', pkg]);\n} catch (e) {\n  if (/workspacesRemoveRootCheck/.test(e.message)) {\n    console.error('Run from a workspace package, or pass -W to remove at root.');\n    return;\n  }\n  throw e;\n}","preventionTips":["Operate on dependencies from within the owning workspace directory.","Only pass `-W` when you intentionally change root-level deps.","Print CWD/workspace info in scripts to avoid accidental root operations."],"tags":["cli","remove","workspaces","safety-guard"],"backgroundTag":null,"analyzedSha":"c2dda503f3759b5be5f0e24ecd9cf5c97a540147","analyzedAt":"2026-08-13T04:17:06.305Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}