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

13.3k views Asked by At

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
2

There are 2 answers

1
Alexander Alexandrov On

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

0
Ahmer Saeed On

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