Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot yarn add in workspace package if depending on a local package #3973

Open
aymericbeaumet opened this issue Jul 20, 2017 · 36 comments · May be fixed by #6012
Open

Cannot yarn add in workspace package if depending on a local package #3973

aymericbeaumet opened this issue Jul 20, 2017 · 36 comments · May be fixed by #6012

Comments

@aymericbeaumet
Copy link

aymericbeaumet commented Jul 20, 2017

Do you want to request a feature or report a bug?

bug

What is the current behavior?

$ yarn add whatever
yarn add v0.27.5
[1/4] Resolving packages...
error An unexpected error occurred: "https://registry.yarnpkg.com/@scope%2fb: Not found".
info If you think this is a bug, please open a bug report with the information provided in "/private/tmp/yarn-issue/packages/a/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.

If the current behavior is a bug, please provide the steps to reproduce.

git clone https://github.com/aymericbeaumet/yarn-workspace-add-package.git /tmp/yarn-issue
cd /tmp/yarn-issue/packages/a
yarn add whatever

What is the expected behavior?

  • The @scope/b should be found as it exists in the workspace
  • The package whatever should be added as a dependency of the @scope/a package

Please mention your node.js, yarn and operating system version.

node 6.10.2
yarn 0.27.5
osx 10.12.5

@pedronauck
Copy link

In the rfc has some notes about workspace command to do that

yarn worspace <package-name> add <dependency>

But I tried in a project and it's doesn't work anyway :sad:

@aymericbeaumet
Copy link
Author

But I tried in a project and it's doesn't work anyway :sad:

What's happening on your side?

I just tried yarn workspace <package-name> add <dependency>, and it seems to work, but the <dependency> is added at the root package.json, instead being appended to the <package> package.json. I'm not sure that's the expected behaviour.

@pedronauck
Copy link

Tha's happened with me too, <dependency> was add on package.json at root. I think that's is a bug, because the package dependency should added on it package.json, right?!

@aymericbeaumet
Copy link
Author

because the package dependency should added on it package.json, right?!

Yep it should 👌, here is the updated bug report


Do you want to request a feature or report a bug?

bug

What is the current behavior?

$ yarn workspace '@scope/a' add whatever
yarn workspace v0.27.5
yarn add v0.27.5
[1/4] Resolving packages...
error An unexpected error occurred: "https://registry.yarnpkg.com/@scope%2fb: Not found".
info If you think this is a bug, please open a bug report with the information provided in "/private/tmp/yarn-issue/packages/a/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.
error Command failed.
Exit code: 1
Command: /usr/local/bin/node
Arguments: /usr/local/bin/yarn add whatever
Directory: /private/tmp/yarn-issue/packages/a
Output:

info Visit https://yarnpkg.com/en/docs/cli/workspace for documentation about this command.

If the current behavior is a bug, please provide the steps to reproduce.

git clone https://github.com/aymericbeaumet/yarn-workspace-add-package.git /tmp/yarn-issue
cd /tmp/yarn-issue/packages/a
yarn workspace '@scope/a' add whatever

What is the expected behavior?

  • The @scope/b should be found as it exists in the workspace
  • The package whatever should be added as a dependency of the @scope/a package

Please mention your node.js, yarn and operating system version.

node 6.10.2
yarn 0.27.5
osx 10.12.5

@BYK
Copy link
Member

BYK commented Oct 27, 2017

This should be fixed on 1.2.1 - can someone confirm?

@flaviouk
Copy link

@BYK It works as expected on 1.2.1

@Aaike
Copy link

Aaike commented Jan 1, 2018

it works as expected but i cannot add local package as a dev dependency.
when running yarn workspace '@scope/a' add whatever -D , the -D argument is stripped away
any argument after the package name is stripped away

@jdcrensh
Copy link

jdcrensh commented Jan 5, 2018

This continues to be an issue as of yarn 1.3.2

$ yarn add @scope/b
yarn add v1.3.2
[1/4] 🔍  Resolving packages...
error An unexpected error occurred: "https://registry.yarnpkg.com/@scope%2fb: Not found".

Project structure

scope-root/package.json

{
  "name": "@scope/root",
  "version": "1.0.0",
  "private": true,
  "workspaces": ["packages/*"]
}

scope-root/packages/module-a/package.json

{
  "name": "@scope/a",
  "version": "1.0.0",
  "private": true
}

scope-root/packages/module-b/package.json

{
  "name": "@scope/b",
  "version": "1.0.0",
  "private": true
}

Issuing either of the following will fail with the "not found" error:

  • Under scope-root/packages/module-a/: yarn add @scope/b
  • Anywhere under scope-root/: yarn workspace @scope/a add @scope/b
    • Note: quoting workspace/dependency names yields the same result

As commented on previously, one would expect either case to work with or without the -D flag, and @scope/b should be appended to the dependencies of @scope/a.

The only workaround is manually editing scope-root/packages/module-a/package.json, then inserting "@scope/b": "1.0.0" as a dependency (or devDependency), and finally running yarn. But none of that should be necessary.

@ghost
Copy link

ghost commented Feb 19, 2018

@Aaike cd to the workspace of choice then yarn add package --dev.

@rally25rs
Copy link
Contributor

rally25rs commented Feb 20, 2018

Still broken in current master (v1.4.1). It looks like yarn add and yarn workspace add check the npm registry, not local workspace packages:

~/Projects/yarn-test 🐒   yarn workspaces info
yarn workspaces v1.4.1
{
  "frontend": {
    "location": "packages/frontend"
  },
  "x_totally_not_real": {
    "location": "libs/x_totally_not_real"
  }
}
✨  Done in 0.08s.

~/Projects/yarn-test 🐒   yarn workspace frontend add x_totally_not_real
yarn workspace v1.4.1
yarn add v1.4.1
[1/4] 🔍  Resolving packages...
error Couldn't find package "x_totally_not_real" on the "npm" registry.
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.
error Command failed.
Exit code: 1

however if you just use yarn install then it works fine:

~/Projects/yarn-test 🐒   rm -rf node_modules/

~/Projects/yarn-test 🐒   yarn install
yarn install v1.4.1
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
[4/4] 📃  Building fresh packages...
success Saved lockfile.
✨  Done in 0.24s.

~/Projects/yarn-test 🐒   ls -l node_modules/
total 24
lrwxr-xr-x  1 user  LOCAL\Domain Users  20 Feb 20 08:20 frontend -> ../packages/frontend
lrwxr-xr-x  1 user  LOCAL\Domain Users  26 Feb 20 08:20 x_totally_not_real -> ../libs/x_totally_not_real

The problem is worse if you local package matches the name of an actual published library, because that will get installed instead of your local one. i.e. if you have a workspace package names base and try to yarn add base then it will download the actual base library from NPM, not use your local workspace one.

@dinvlad
Copy link

dinvlad commented Feb 23, 2018

@flagello Unfortunately, running yarn add from inside a workspace breaks Yarn hooks (e.g. patch-package) that normally run inside the root directory.

It would be best to just run yarn <workspace> add -D <package> (where yarn <workspace> is a shortcut script for yarn workspace <workspace> that one can use today)

@ghost
Copy link

ghost commented Feb 23, 2018

@dinvlad yarn workspace <workspace> add -D <package> should be yarn workspace <workspace> add <package> -D.

Still, as of v1.3.2, yarn workspace <workspace> add <package> --dev adds <package> to the workspace’s dependencies rather than its devDependencies (#5396).

@dinvlad
Copy link

dinvlad commented Feb 23, 2018

@flagello any reason for the exact ordering? after all, regular yarn add accepts both -D <package> and <package> -D forms.

@dinvlad
Copy link

dinvlad commented Feb 23, 2018

Alternatively, we could use yarn --cwd <workspace> add <package> -D (if that worked properly), but that probably deserves another issue.

@ghost
Copy link

ghost commented Feb 23, 2018

Sorry @dinvlad, I forgot to mention yarn workspace <workspace> add -D <package> specifically fails with Missing list of packages to add to your project. I did not attempt to reproduce elsewhere. Might be related to these outstanding issues?

@dinvlad
Copy link

dinvlad commented Feb 23, 2018

@flagello ah yes, I recall that happened to me to. Thanks for pointing this out. Both variants should work once a fix is implemented, IMO

@elmasse
Copy link

elmasse commented May 7, 2018

One thing that might be related (if anyone can confirm) I have the same issue trying to run a global add from a workspace (trying to global install a workspace module)

I tried these from the workspace folder:

yarn workspace <workspace> global add .
yarn workspace <workspace> global add <workspace>
yarn global add .

None of those worked. Got the same error, yarn tries to get the local package dependencies from yarnpkg registry getting an error of malformed response.

yarn 1.6.0
macOS 10.13.4
node 9.3

rally25rs added a commit to rally25rs/yarn that referenced this issue Jun 20, 2018
…pace packages

Previously for a requested dependency pattern to match a package in a workspace, it's name had to
match and so did the semver range. If a request specified a non-semver range (like a github url)
then it would not match, causing the resolver to attempt to resolve the package on the registry
instead of use the workspace package. This change will match a workspace package just on it's name
if no valid semver range was found on the requested pattern.

fixes yarnpkg#4878
fixes yarnpkg#3973
affects yarnpkg#5726
@arcanis
Copy link
Member

arcanis commented Jun 21, 2018

Still, as of v1.3.2, yarn workspace <workspace> add <package> --dev adds <package> to the workspace’s dependencies rather than its devDependencies (#5396).

I believe yarn workspace <workspace> -- add <package> --dev should work. The -- is necessary to split the yarn workspace options and the yarn add options.

@dinvlad
Copy link

dinvlad commented Jun 21, 2018

In fact, I've tried yarn workspace <workspace> add -D <package> and that works too with Yarn 1.5.1

@gagarin55
Copy link

I'm using Lerna monorepo with yarn.
I can't do yarn add somepkg because yarn always wants resolve my local packages through npm
registry ;(

v1.13.0

Please, fix it somehow ;((((

@HopperGithub
Copy link

I've tried yarn workspace add and that not works too with Yarn 1.19.1

yarn workspace @scope/a add @scope/b@1.0.0

error An unexpected error occurred: "https://registry.yarnpkg.com/@scope%2fb: Not found".

Thanks in advance for any help!

@mykyta-shyrin
Copy link

Same problem for me

@bearcanrun
Copy link

bearcanrun commented Feb 11, 2020

Using yarn 1.220: workspaces + lerna

yarn workspace <workspace> add <package> -D or any variant above always adds as dependency

However, if I use lerna add <package> --dev --scope <workspace> it properly adds to devDependencies.

Lerna is using Yarn to install it, but not sure what the underlying difference is. Here's output from running above lerna command:

info cli using local version of lerna
lerna notice cli v3.20.2
lerna info versioning independent
lerna notice filter including "my-site"
lerna info filter [ 'my-site' ]
lerna info Adding my-svelte-component-library in 1 package
lerna info bootstrap root only
yarn install v1.21.1
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
[4/4] 🔨  Building fresh packages...
✨  Done in 11.11s.

edit: Just noticed that lerna is using different yarn version than what I get from running:

$ yarn --version
1.22.0

@ericwooley
Copy link

ericwooley commented Feb 12, 2020

cd package/whatever; yarn add typescript --dev
yarn workspace whatever add typescript --dev
yarn workspace whatever -- add typescript --dev

yarn workspace whatever add typescript --dev
yarn workspace v1.22.0
yarn add v1.22.0
info No lockfile found.
$ node ./internals/scripts/xcodecheck.js
[1/5] Validating package.json...
[2/5] Resolving packages...
error An unexpected error occurred: "https://registry.yarnpkg.com/local-dep: Not found".
info If you think this is a bug, please open a bug report with the information provided in "/home/ericwooley/projects/project/whateveryarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.
error Command failed.
Exit code: 1
Command: /home/ericwooley/.nvm/versions/node/v12.13.0/bin/node
Arguments: /home/ericwooley/projects/project/.yarn/releases/yarn-1.22.0.js add typescript --dev
Directory: /home/ericwooley/projects/project/whatever
Output:

info Visit https://yarnpkg.com/en/docs/cli/workspace for documentation about this command.

All broken on 1.22.0, 1.21.0, and 1.16.0 for me in ubuntu and osx.

Manually adding packages is very annoying :(

@LironEr
Copy link

LironEr commented Mar 25, 2020

I got this error when my paths were with ./

"workspaces": [
    "./web",
    "./desktop",
    "./packages/*"
  ],

After I removed ./ everything worked

"workspaces": [
    "web",
    "desktop",
    "packages/*"
  ],

@mgtitimoli
Copy link

mgtitimoli commented Apr 29, 2020

I landed here searching for the error I got, and after trying a couple of solutions, I came up with another not yet described, it seems yarn is taking into account the version of the packages in the monorepo, so if instead of just trying to add them we append @<version>, it works.

This doesn't work

$ yarn add @myscope/package

But adding the version works fine
This doesn't work

$ yarn add @myscope/package@1.0.0

@patrickwall57
Copy link

I got this error when my paths were with ./

"workspaces": [
    "./web",
    "./desktop",
    "./packages/*"
  ],

After I removed ./ everything worked

"workspaces": [
    "web",
    "desktop",
    "packages/*"
  ],

THANK YOU

@Muhammad-Alaa-Kareem
Copy link

I was trying to add expo-cli using yarn add -g expo-cli but I had the error mentioned. When I used yarn add global expo-cli it worked.

@ziyoung
Copy link

ziyoung commented Jan 23, 2021

Using yarn 1.220: workspaces + lerna

yarn workspace <workspace> add <package> -D or any variant above always adds as dependency

However, if I use lerna add <package> --dev --scope <workspace> it properly adds to devDependencies.

Lerna is using Yarn to install it, but not sure what the underlying difference is. Here's output from running above lerna command:

info cli using local version of lerna
lerna notice cli v3.20.2
lerna info versioning independent
lerna notice filter including "my-site"
lerna info filter [ 'my-site' ]
lerna info Adding my-svelte-component-library in 1 package
lerna info bootstrap root only
yarn install v1.21.1
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
[4/4] 🔨  Building fresh packages...
✨  Done in 11.11s.

edit: Just noticed that lerna is using different yarn version than what I get from running:

$ yarn --version
1.22.0

It works for me. If you are using mono repo, you'd better install lerna in root workspace. Use lerna to install a local package.

@fbessou
Copy link

fbessou commented Mar 19, 2021

In my case the problem was with the presence of trailing slashes in the workspaces declarations:

"workspaces": [
  "packages/p1/",
  "packages/p2/"
]

It worked when I removed the trailing slashes:

"workspaces": [
  "packages/p1",
  "packages/p2"
]

@SahidMiller
Copy link

I got this error when my paths were with ./

"workspaces": [
    "./web",
    "./desktop",
    "./packages/*"
  ],

After I removed ./ everything worked

"workspaces": [
    "web",
    "desktop",
    "packages/*"
  ],

In a similar vein: packages\\* will not work, while packages/* does.

You'll get an unrelated error message:

Couldn't find package "@scope/project1@0.0.0" required by "@scope/project2@link:packages/project2" on the "npm" registry. unless changing that. And surprisingly, only on CI builds for me.

@Motirck
Copy link

Motirck commented Jul 4, 2021

I had a similar error running the command below.

$ yarn add --save-dev @types/sequelize

As the dependency has to be installed only in the development environment, I tried to run it with --save-dev but apparently this would work if I had been using NPM. This replaces this command snippet with -D

$ yarn add @types/sequelize -D

@tobico
Copy link

tobico commented Jan 6, 2022

I just ran into this issue and the solution which worked for me was to upgrade to Yarn 2 using the instructions here: https://yarnpkg.com/getting-started/migration

@robng
Copy link

robng commented Oct 27, 2022

wtf @yarnpkg?

there's clearly a problem going on here.

I landed here searching for the error I got, and after trying a couple of solutions, I came up with another not yet described, it seems yarn is taking into account the version of the packages in the monorepo, so if instead of just trying to add them we append @<version>, it works.

This doesn't work

$ yarn add @myscope/package

But adding the version works fine This doesn't work

$ yarn add @myscope/package@1.0.0

@joeheyming
Copy link

@LironEr is my hero 🏆

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.