Yarn add —dev returns root warning - yarnpkg

When I use yarn add —-dev I receive the warning “Running this command will add the dependency to the workspace root”
Which makes no sense, since the
—-dev flag is supposed to add it to only workspace
Running yarn add -dev returns the yarn version.
I’ve tried uninstalling and reinstalling yarn, but the error continues.
I can’t find any info on this problem and I don’t have any idea how to fix it, can someone provide some advice?

When you run yarn add --dev <package> is supposed to add package as development dependency. To add dependency to a specific workspace, cd to that workspace and run yarn add there.

Related

getting YN0028 The lockfile would have been modified by this install, which is explicitly forbidden. using yarn berry and heroku

I'm using yarn berry and heroku and consistently getting the error:
➤ YN0028: │ The lockfile would have been modified by this install, which is explicitly forbidden.
Which suggests that my lockfile does not contain all my listed dependencies. In the yarn docs it says this is easily solved by running yarn install and pushing new lockfile to git. However I've tried this, tried with fresh node_modules, etc with no luck.
Has anyone else experienced this issue using yarn berry + heroku?
My repo is a monorepo using workspaces.
I was able to workaround by setting the env-var YARN_ENABLE_IMMUTABLE_INSTALLS to false, as suggested here.
This is likely a bug in Yarn Berry. I've reported it here: https://github.com/yarnpkg/berry/issues/2948
UPD: I have created a fresh local clone of the repo from GitHub, ran yarn install in it, and it did produce changes in yarn.lock. Committing those changes resolved the CI issue, so disabling YARN_ENABLE_IMMUTABLE_INSTALLS is no longer necessary for me.
The original local repo showed a clean git status, so I still believe it is a bug.
UPD 2: My problem was that one of the Yarn worspaces was checked into git as a git submodule (I have probably created it with a nested .git/ folder and then deleted it). As a result, the workspace content, including a child package.json was not committed into the repo, it only existed in my local repo and not on the remote and CI.
After removing the git submodule and checking the workspace into the repo properly, the YN0028 error stopped happening.
If your ENV doesn't contain any CI variables:
Then it could be your yarn config:
Run yarn config get enableImmutableInstalls and see if it's enabled.
(you can also check why it is enabled by running yarn config --why and looking for enableImmutableInstalls).
If it is true, then run yarn config set -H enableImmutableInstalls false to set the setting's value globally (or without the -H argument to set it only in your current project)
I ran across the same issue. I resolve dit by deleting my cache and then reinstalling the dependencies.
The yarn.lock file was then modified by the time the reinstall had completed.
I believe this may have been because I checked in the cache folder initially, and then reverted it. Not sure if this then caused a discrepancy between my local environment and the checked-in repo.

yarn berry run how to run installed packages

I see with yarn berry I get the plug'n'play feature instead of node_modules/
I couldn't find anything to suggest it supports running from installed packages.
For example with npm a workflow might be to run the installed version of webpack:
$ npm install --save-dev webpack
$ node node_modules/webpack/bin/webpack ...
A globally installed webpack might not be the same version. Worse yet, during Docker deployment, I get what's installed locally, the only node and npm are available globally. I thought I can do a preinstall script that does npm install -g yarn; yarn set version berry but then I'm not sure how to do webpack, jest, babel, etc, and the thought that I should have to install them all globally during the same preinstall hackaround seems like several steps backwards.
Is there some way to run from locally-installed packages that I'm missing?
I saw this possibly related question - Yarn Berry - Run a Node Script Directly
But the answer there seems a bit off the point - I'm not running any js, I'm trying to type in a package.json script, i.e. something that can run from the shell.
Why not just use yarn run <bin> (or simply yarn <bin>)? If you are in a repository set to use yarn berry, that will run any package bin file.
yarn node <file> will run any .js file with Plug n' Play set up. No need to install those dependencies globally, except for maybe yarn classic.
I was trying to do yarn some-bin and kept getting:
Couldn't find a script named "some-bin".
I eventually figured out it was because the package that provides some-bin is installed inside a workspace and not at the root of my project. So instead I had to run:
yarn workspace my-workspace some-bin
And that worked.

Yarn install has been replaced with `add`

On my Windows system I can run yarn install with no issue in my project. But during my Azure build which is running on Ubuntu-16.04 I get the following message:
error: install has been replaced with add to add new dependencies. Run "yarn add yarn build" instead.
Doing a yarn add gives this message:
error: Running this command will add the dependency to the workspace root rather than the workspace itself, which might not be what you want - if you really meant it, make it explicit by running this command again with the -W flag (or --ignore-workspace-root-check).
In my project I have multiple applications all with their own package.json file. If I'm reading the message correctly the yarn add will add all the dependencies to the root file and not in the directories where the package.json files are located.
So how do install the packages per directory/package.json file using yarn add?
Initially I added: yarn add --cwd apps/<foldername>/<foldername> to the build script. You can do this for multiple folders to initiate different builds. But just running yarn from the root also resolved all the different builds.

Yarn install trigger all scripts in my package.json, it is normal?

The documentation doesn't mention this specifity:
yarn install is used to install all dependencies for a project. This
is most commonly used when you have just checked out code for a
project, or when another developer on the project has added a new
dependency that you need to pick up.
Yarn install : Install all the dependencies listed within package.json in the local node_modules folder.

How to install all the dependency package in yarn?

First, I'm new to React. I'm trying to use Google's Material-UI for my React project. In this tutorial, it says run npm install, but I heard using yarn and npm together in the same project because it might bring about some confusion between those two later. So, I'm trying to stick to yarn only.
npm install seems to install all the dependency package for the thing that I wanna use, but how can I do that in yarn? I tried yarn add, but it didn't work. How can I do that?
EDIT
Just found that it has only package.json, which means I can only use npm install to install dependencies. Would there be no problem when I use yarn later?
You should just be able to run yarn ("Running yarn with no command will run yarn install, passing through any provided flags." So just a simple yarn is what you'd want now, this answer previously suggested yarn install)
Here is a comparison table of most/all the commands you'd likely encounter
Edit Sept 2020: The newer versions of npm have greatly improved and caught up to yarn, so I currently have no clue what possible benefits yarn offers anymore, I'm 100% npm for the last year or so
The equivalent of $ npm install is just $ yarn (without arguments) to install all dependencies from package.json.
Also, just to clarify your query -
Just found that it has only package.json, which means I can only use npm install to install dependencies. Would there be no problem when I use yarn later?
Doesn't matter whether you use yarn or npm, there will always be a package.json.
And no, there won't be a problem when using yarn later.
You get package-lock.json when using npm, and yarn.lock file when using yarn.
It's not recommended to use both yarn and npm for the same project, so you should remove either of package-lock.json and yarn.lock.
In my case it worked as - yarn global add #angular/cli (similarly any dependency)
then added path "C:\Users\USER_NAME\AppData\Local\Yarn\Data\global\node_modules.bin" in "Edit the system environment variables" -> Environment Variables -> Under System Variables select PATH-> click New -> Add the above path then save-> Open new command prompt -> run the dependency command.
yarn install --force
From Yarn's help text on the install command:
--force   install and build packages even if they were built before, overwrite lockfile
I had a situation where nom install would install everything and yarn install wouldn't. So maybe try the other package manager?

Resources