Yarn 2 init, add failing
Asked Answered
E

6

20

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?

Exodontics answered 24/9, 2020 at 14:40 Comment(0)
B
23

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.

Batter answered 30/9, 2020 at 14:45 Comment(1)
As per the docs, I should do this 1. yarn init, 2. yarn, 3. yarn install. If the next step (yarn/yarn install) would want me to delete the package.json or yarn.lock file, why would it make me create it in the first place by running yarn init?Koon
K
38

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

Katharyn answered 11/2, 2021 at 8:14 Comment(3)
damn why cant yarn just look if .lock doesn't exist then create it like you did?Phthalein
this never "confused" yarn before..Crypto
I had the same error with workspaces. I created a new workspace, yarn init ran, but yarn add <...> failed for new dependencies. Adding the yarn.lock file solved the issue.Amulet
B
23

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.

Batter answered 30/9, 2020 at 14:45 Comment(1)
As per the docs, I should do this 1. yarn init, 2. yarn, 3. yarn install. If the next step (yarn/yarn install) would want me to delete the package.json or yarn.lock file, why would it make me create it in the first place by running yarn init?Koon
C
5

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

Cocainize answered 4/4, 2021 at 11:24 Comment(1)
Thanks! I just ran into this bug, now I wonder that yarn set berry means lolEmarie
H
3

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!

Haugen answered 6/11, 2020 at 23:16 Comment(0)
T
0

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.

Tracee answered 7/8, 2022 at 3:10 Comment(0)
L
0

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.

Longboat answered 25/9, 2023 at 7:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.