Why does Yarn throw "Invariant Violation: expected workspace package to exist" when I attempt to upgrade some dependencies?
Asked Answered
W

3

18

I have a monorepo that is managed by which relies on the Yarn workspaces feature. It looks like this:

/repo
  |-- packages
  |   |-- pkg-a
  |   |   |-- package.json
  |   |-- pkg-b
  |   |   |-- package.json
  |   |-- pkg-c
  |   |   |-- package.json
  |-- package.json

I use yarn upgrade-interactive to update a selection of dependencies across my packages but I get this error:

Invariant Violation: expected workspace package to exist for "@babel/plugin-transform-classes"

However this isn't a direct dependency of mine. When I search for it in all my package.json files I get nothing:

$ find . -name "package.json" -and -not -path "*/node_modules/*" | xargs grep "@babel/plugin-transform-classes"
$

It is present in my yarn.lock file though:

$ grep "@babel/plugin-transform-classes" yarn.lock
"@babel/plugin-transform-classes@^7.10.4":
  resolved "https://registry.yarnpkg.com/@babel/plugin-transform-classes/-/plugin-transform-classes-7.10.4.tgz#405136af2b3e218bc4a1926228bc917ab1a0adc7"
"@babel/plugin-transform-classes@^7.4.0":
  resolved "https://registry.yarnpkg.com/@babel/plugin-transform-classes/-/plugin-transform-classes-7.4.0.tgz#e3428d3c8a3d01f33b10c529b998ba1707043d4d"
    "@babel/plugin-transform-classes" "^7.4.0"
    "@babel/plugin-transform-classes" "^7.10.4"
$

I suspected some sort of caching issue so I tried to start from scratch inside a Docker container:

  1. Delete all node_modules folders
  2. Mount the repo in a Docker container
  3. Run yarn to install all dependencies
  4. Run yarn upgrade-interactive

But I get the exact same error.

I went scavenging for answers and none have fixed my issues. What strikes me the most is that nobody seems to know why it is happening.

Can anybody explain why it is happening and how one could reproduce this issue? Once we know why, we stand a better chance to find a proper solution.

Technical details:

  • Node 12.18
  • Yarn 1.22
  • Lerna 3.20
Winwaloe answered 25/9, 2020 at 8:5 Comment(2)
What does yarn why @babel/plugin-transform-classes say?Convulsion
@Convulsion it says that one of my sub packages has a dependency that requires it. That dependency isn't in the nohoist config.Winwaloe
T
6

It seems that it is a bug

https://github.com/yarnpkg/yarn/issues/7734#issuecomment-580012389

And the official recommendation is to downgrade to Yarn 1.19.x or upgrade packages manually (Yarn 1 is deprecated).

https://github.com/yarnpkg/yarn/issues/7734#issuecomment-671729912

Also, you could try to use https://www.npmjs.com/package/npm-check-updates CLI utility

Tenaculum answered 15/7, 2021 at 9:34 Comment(1)
This solution helped me with a project I was working on. After deleting the node_modules folder and reinstalling with yarn 1.19.1. The invariant violation errors are no longer preventing me from installing with yarn addAntinomian
R
1

Try after removing the packages from resolution section of the package.json file which you want to upgrade.

Rubie answered 28/8, 2022 at 9:54 Comment(0)
E
0

you can manually adjust the package.json and run yarn install. - DalderupMaurice

https://github.com/yarnpkg/yarn/issues/7734#issuecomment-580364381

This worked for me. Just manually add the packages to package.json, delete node_modules and run yarn install

Cross posting here to save people the time from having to parse through the yarn issue, thanks DalderupMaurice!

Emmenagogue answered 30/4, 2024 at 15:52 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.