yarnpkg/yarn · error · MessageError

Directory $0 doesn't exist

Error message

Directory $0 doesn't exist

What it means

Thrown by `findWorkspaceRoot` when the initial cwd path does not exist on disk (`fs.exists(current)` is false). Before walking up the tree looking for a workspace root, Yarn confirms the starting directory is real; if it is not, it fails fast rather than walking phantom paths.

Source

Thrown at src/config.js:777

  }

  async findManifest(dir: string, isRoot: boolean): Promise<?Manifest> {
    for (const registry of registryNames) {
      const manifest = await this.tryManifest(dir, registry, isRoot);

      if (manifest) {
        return manifest;
      }
    }

    return null;
  }

  async findWorkspaceRoot(initial: string): Promise<?string> {
    let previous = null;
    let current = path.normalize(initial);
    if (!await fs.exists(current)) {
      throw new MessageError(this.reporter.lang('folderMissing', current));
    }

    do {
      const manifest = await this.findManifest(current, true);
      const ws = extractWorkspaces(manifest);
      if (ws && ws.packages) {
        const relativePath = path.relative(current, initial);
        if (relativePath === '' || micromatch([relativePath], ws.packages).length > 0) {
          return current;
        } else {
          return null;
        }
      }

      previous = current;
      current = path.dirname(current);
    } while (current !== previous);

View on GitHub (pinned to c2dda503f3)

Solutions

  1. Verify the directory exists before running Yarn: `test -d "$PWD" || cd ~`.
  2. Recreate or `cd` into a valid directory before invoking Yarn.
  3. Check that any `--cwd` flag points to an existing path.
  4. Avoid deleting the cwd in a preinstall/postinstall hook.

Example fix

# before
yarn install  # cwd was just rm -rf'd
# after
cd /valid/project/dir
yarn install
Defensive patterns

Strategy: validation

Validate before calling

const fs = require('fs');
const cwd = process.cwd();
if (!fs.existsSync(cwd)) {
  console.error(`cwd '${cwd}' does not exist. Re-run from a valid directory.`);
  process.exit(1);
}

Try / catch

try {
  await config.findWorkspaceRoot(initial);
} catch (err) {
  if (err instanceof MessageError && err.message.includes("doesn't exist")) {
    process.chdir(process.env.HOME || '/tmp');
    throw new Error(`Working directory was removed — restarted in a valid cwd.`);
  }
  throw err;
}

Prevention

When it happens

Trigger: The process cwd was deleted, unmounted, or renamed out from under the running Yarn process. Passing a non-existent `--cwd` value. A script `cd`s into a dir then it is removed before Yarn starts.

Common situations: A build step deletes and recreates the working directory while Yarn is spawned. tmux/IDE session retains a stale cwd after the dir was removed. Typo in a wrapper script's `cd` target.

Related errors


AI-assisted analysis of yarnpkg/yarn@c2dda503f3 (2026-08-13). Data as JSON: /api/errors/b50de3eeb92a285e. Report an issue: GitHub.