yarn link fails: "Can't link the project to itself" when trying to link to a package in a separate folder. After upgrading from yarn 1.x
Self-service
- [X] I'd be willing to implement a fix
Describe the bug
I have two folders A and B with packages created with yarn init.
In A I run yarn link.
In B I run yarn link A.
I expect being able to use A in B afterwards. This worked in yarn 1.x as far as I know.
However in yarn 4.3.0 I get this error instead:
Usage Error: Invalid destination 'A'; Can't link the project to itself
To reproduce
The following script reproduces the issue on my computer.
mkdir A
mkdir B
cd A
yarn init -n A
yarn link
cd ../B
yarn init -n B
yarn link A
Environment
System:
OS: Linux 6.8 Manjaro Linux
CPU: (12) x64 AMD Ryzen 5 PRO 5650U with Radeon Graphics
Binaries:
Node: 18.18.0 - /tmp/xfs-8284f6e0/node
Yarn: 4.3.0 - /tmp/xfs-8284f6e0/yarn
npm: 9.8.1 - ~/.nvm/versions/node/v18.18.0/bin/npm
Additional context
No response
Okay, I read the command help and it seems like the behaviour of link has changed from yarn 1.x
━━━ Details ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
This command will set a new `resolutions` field in the project-level manifest
and point it to the workspace at the specified location (even if part of another
project).
━━━ Examples ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Register one or more remote workspaces for use in the current project
$ yarn link ~/ts-loader ~/jest
Register all workspaces from a remote project for use in the current project
$ yarn link ~/jest --all
After running yarn link ../A from B I have
{
...
"resolutions": {
"A": "portal:/<whatever>/A"
}
}
in my B's package.json.
Unfortunately neither rollup or vite dev can find that external package A in my real world case.
I could go back to yarn 1.x but I need >3 for importing sharp 0.33. So I find myself in one of these lovely version mismatch situations. I'd like to find a way to do yarn 1.x link in yarn >3
So yea, I'll leave this open, maybe someone has a good idea how to do what yarn 1.x link did in yarn >3.
Thanks
Well, for now I'm just putting A into the alias section of my vite config:
alias: {
...
'A': path.resolve(__dirname, '../A/src'),
}
That works only for vite projects obviously. And you might need to change the .../src part of the path.
Let me take a step back and explain what I want to achieve:
I'm developing an npm package. I'm developing two projects which use that npm package.
On my machine, during development, I'd like the two projects to use the local state of the npm package.
How to achieve that with yarn berry?
(I don't want a monorepo)
using npm link just like yarn 1.x's link for now.
Works in combination with yarn 4.3.0
After running yarn link ../A from B I have
Do you have a dependency or devDependency to A in addition to the resolutions?
Hi! 👋
It seems like this issue as been marked as probably resolved, or missing important information blocking its progression. As a result, it'll be closed in a few days unless a maintainer explicitly vouches for it.
I'm too lazy to investigate since using npm link works just fine.
Same problem in 4.5.0
Would be great if this issue could be re-opened since yarn link is essentially broken on 4.5.0. This makes local package development very tricky using the workflow @felixniemeyer outlined above in https://github.com/yarnpkg/berry/issues/6331#issuecomment-2162775747.
Let's summarize everything that has been said in this thread so far:
yarn link's signature changed between v1 and v2.0.0. Now it's a one-step process by simply giving it the path(s) (relative or absolute) of the packages to link to.yarn linkusesresolutionsto force all packages of the linked name(s) in the dependency tree to resolve to the linked package, but by itself does not cause the project to depend on that package. Useyarn add (--dev)to do that.
Thus, if your project doesn't already depend on the linked package, the workflow to dev-depend on it while developing it locally is:
$ yarn link ../path/to/linked-package
$ yarn add --dev linked-package-name@^1.0.0
If that doesn't work as expected, please open a new issue with reproduction so we can take a look
Thank you @clemyan that works as expected.
This is an essential workflow and the error message when you do it wrong is totally misleading. Also no helpful documentation is provided on the yarn link page.
Fix your website's SRP, as yarn 1's doc rank first for "yarn 4" related queries: