Nono.MA

FEBRUARY 14, 2024

I kept seeing this error when creating a new Yarn Modern project—setting Yarn to the latest version with yarn set version stable—and using the yarn init command.

Usage Error: The nearest package directory doesn't seem part of the project declared in […]

For instance, yarn add -D @types/node wouldn't work.

The fix

There was a package.json and a yarn.lock file in my home directory.

Removing the file fixed the issue.

rm ~/package.json ~/yarn.lock

Create a Yarn Modern project

Then, in any directory, even subfolders of your home directory (~) you can create new yarn projects.

mkdir app && cd app
yarn init -y
yarn add -D @types/node

Doing It Right

When you run yarn set version stable, Yarn Modern creates a package.json with the packageManager property set to the latest stable version of Yarn, such as 4.1.0.

To avoid the above issue, you should first create your project.

yarn create vite my-app --template react-swc-ts

Then, enter the app's directory and only then set the desired Yarn version.

cd my-app
yarn set version stable
yarn
# Yarn Modern will be used here.

Want to see older publications? Visit the archive.

Listen to Getting Simple .