Skip to content

/errors/yarn/nearest-package-directory-not-part-of-project

Yarn: Usage Error: The nearest package directory doesn't seem to be part of the project declared in

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated
tool
Yarn
exit code
1
versions
Yarn 2 – 4
os
any

Usage Error: The nearest package directory (...) doesn't seem to be part of the project declared in ... means Yarn 2 or later (Berry) walked up from the current folder, found a project root higher up (a folder with yarn.lock), and the current package is not one of that project's workspaces. Yarn refuses to guess. Either remove the stray yarn.lock in the parent, list the folder under workspaces in the parent package.json, or create an empty yarn.lock in the current folder to make it a separate project.

Reproduce it

bash
mkdir -p sub
echo '{ "name": "root" }' > package.json
touch yarn.lock
echo '{ "name": "sub" }' > sub/package.json
cd sub
npx --yes @yarnpkg/cli-dist@4 install 2>&1 | head -1
exit "${PIPESTATUS[0]}"

Output · exit 1

Usage Error: The nearest package directory (/tmp/work/sub) doesn't seem to be part of the project declared in /tmp/work.

Yarn follows the message with three suggestions, one per fix below, and the usage line of the command.

Fix

Make the subfolder its own project with an empty lockfile:

bash
mkdir -p sub
echo '{ "name": "root" }' > package.json
touch yarn.lock
echo '{ "name": "sub" }' > sub/package.json
cd sub
touch yarn.lock
npx --yes @yarnpkg/cli-dist@4 install >/dev/null && echo "installed as a separate project"

Output

installed as a separate project

Or, in a monorepo, declare it as a workspace in the root package.json and install from the root:

package.json
{
  "name": "root",
  "private": true,
  "workspaces": ["sub", "packages/*"]
}

Sources