Yarn 2 init, add failing

Viewed 9297

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?

5 Answers

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

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.

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

It seems to be that yarn.lock file is what is 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 now if you want to add any package by yarn add [whatever] you will fail with an error. Now you need to touch yarn.lock (or on windows just cat '' > yarn.lock) after that it will work fine.


I figured out an even better / faster way to do it now, just do 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

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!

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.

Related