Reputation: 415
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
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:
Enable corepack if you already didn't:
corepack enable
Change corepack's good known repo for the yarn from v1
to stable
:
corepack install -g yarn@stable
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
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
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
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
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
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
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