Dan Pouliot
Dan Pouliot

Reputation: 415

Yarn 2 init, add failing

I'm experimenting with yarn 2 and faceplanting.

I created a new folder: /projects/yarn2/

As per their install guide https://yarnpkg.com/getting-started I ran

cd /projects/yarn2/
yarn set version berry
yarn init

then (as per their guide https://yarnpkg.com/getting-started/usage )

yarn add react

and got the following error:

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

- If the project directory is right, it might be that you forgot to list yarn2 as a workspace.
- If it isn't, it's likely because you have a yarn.lock or package.json file there, confusing the project root detection.

$ yarn add [--json] [-E,--exact] [-T,--tilde] [-C,--caret] [-D,--dev] [-P,--peer] [-O,--optional] [--prefer-dev] [-i,--interactive] [--cached] ...

What am I doing wrong?

Upvotes: 21

Views: 20911

Answers (7)

Farzan
Farzan

Reputation: 935

I hit this error message while installing NextJS (yarn create next-app). Of course the workaround as others have stated could be adding an empty yarn.lock file into your project's root directory.

However, the best approach to avoid this issue IMHO is:

  1. Enable corepack if you already didn't:

    corepack enable

  2. Change corepack's good known repo for the yarn from v1 to stable:

    corepack install -g yarn@stable

  3. No need for yarn set version berry from now on and therefore no package.json nor .yarnrc files will be generated in the upper directories of your project.

Upvotes: 1

sunny
sunny

Reputation: 11

Try to check if there is a package.json/yarn.lock file in the /project directory, I have had this problem before, and when I removed these files, my problem was solved.

Upvotes: 0

Csaba K.
Csaba K.

Reputation: 166

To add to the previous answers what worked for me was:

It seems to be that yarn.lock file is what's really required, so if you have started with yarn v1.x and did yarn init - then you will have a package.json file in the project dir already.

If you now switch over to yarn v2 by doing yarn set berry, and want to add a package by yarn add [whatever] it will fail. You can work around that by creating an empty file by: touch yarn.lock (or on windows just cat '' > yarn.lock) after that it should work fine.


I figured out an even better / faster way to do it now, just do this in an empty project folder: yarn init -2 this will initialize a new yarn v2 project folder with .yarnrc.yml, package.json, and the .yarn folder, also initializes an empty .git folder and adds a proper .gitignore. check by yarn --version - should echo v2.x.x

Upvotes: 5

Ravinsan
Ravinsan

Reputation: 1

you have to remove yarn.lock and package.json file in your root directory ,some times there may be .yarnrc yarnyml files in you root directory you have to remove them also.

Upvotes: 0

Aryan Pitliya
Aryan Pitliya

Reputation: 1485

You either don't have package.json or yarn.lock which confuses yarn if the package is added in the workspace or not. Just run the following command and I think your problem must be solved.

cd <folder_name>/<project_name>

touch yarn.lock

yarn

Upvotes: 41

Felipe Chernicharo
Felipe Chernicharo

Reputation: 4547

Usage Error: The nearest package directory (/<path_to_folder>/<project_name>) doesn't seem to be part of the project declared in /<path_to_folder>.

Solution: Search for yarn.lock and package.json files inside /<path_to_folder> and delete them!

Upvotes: 3

autoboxer
autoboxer

Reputation: 1397

Check to see if you have a package.json or yarn.lock file in your /projects directory. If you do, clear it/them out and this should start working.

Upvotes: 24

Related Questions