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

Yarn link doesn't work #1297

Open
RusPosevkin opened this issue Oct 20, 2016 · 81 comments
Open

Yarn link doesn't work #1297

RusPosevkin opened this issue Oct 20, 2016 · 81 comments
Labels

Comments

@RusPosevkin
Copy link

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

What is the current behavior?

foo@bar:~/code/lpserver/frontend/addons/lp-base$ yarn link
yarn link v0.16.1
success Registered "lp-base".
info You can now run `yarn link "lp-base"` in the projects where you want to use this module and it will be used instead.
Done in 0.05s.
foo@bar:~/code/lpserver/frontend/addons/lp-base$ cd ../../apps/partner/
foo@bar:~/code/lpserver/frontend/apps/partner$ yarn link lp-base
yarn link v0.16.1
success Registered "lp-base".
Done in 0.05s.
foo@bar:~/code/lpserver/frontend/apps/partner$ yarn
yarn install v0.16.1
info No lockfile found.
[1/4] Resolving packages...
error Couldn't find package "lp-base" on the "npm" registry.
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.

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

What is the expected behavior?

foo@bar:~/code/lpserver/frontend/addons/lp-base$ yarn link
yarn link v0.16.1
success Registered "lp-base".
info You can now run `yarn link "lp-base"` in the projects where you want to use this module and it will be used instead.
Done in 0.05s.
foo@bar:~/code/lpserver/frontend/addons/lp-base$ cd ../../apps/partner/
foo@bar:~/code/lpserver/frontend/apps/partner$ yarn link lp-base
yarn link v0.16.1
success Registered "lp-base".
Done in 0.05s.
foo@bar:~/code/lpserver/frontend/apps/partner$ yarn
yarn install v0.16.1
info No lockfile found.
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
Done in 0.46s.

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

node 6.4.0
yarn 0.16.1
Ubuntu 16.04 LTS
@RusPosevkin
Copy link
Author

Probably, there is a bug when linked package name include the hyphen. The same problem may be in #1161.

@ssteinerx
Copy link

Same problem with package with no hyphen in name; it still looks for package in on the "npm" registry.

Dead in the water without this working on my current project.

@jampy
Copy link

jampy commented Nov 22, 2016

Any news on this?

I think I'm having the same issue (at least the result is the same).

If it helps, some details regarding my situation:

  • building inside a Docker container, as "root", within a VM in Windows
  • I have three packages locally under /src-protected/tonidb/packages named tonidb, tonidb-odm and tonidb-server. The last two depend on tonidb and thus are linked using yarn link tonidb. This works as the integrated tests succeed (yarn run test), so linking somehow works.
  • The main application under /src-protected/winfarmer-xt/src is linked to the three packages just mentioned, but fails with errors like Couldn't find package "tonidb" on the "npm" registry.. The package name in the error message randomly switches between one of the three package names - I guess that's simple because of the parallel install.
  • Since the tonidb* packages are unreleased to NPM yet, I've added them to the package.json files simply like "tonidb": "*".
  • Yarn version is v0.17.8.

I have no idea why the link works for the dependencies themselves but not for the main application.

@jampy
Copy link

jampy commented Dec 16, 2016

Hmmm, anybody found a workaround?

Would like to use Yarn, but this is a major blocker :(

@cdomigan
Copy link

Yarn link seems completely broken? Would be great to get an update from the devs on this. Is anybody having luck using link at all?

@RusPosevkin
Copy link
Author

RusPosevkin commented Mar 27, 2017

Hey guys, let's try to use symbolic linking instead

  $> ln -nsf ../foo node_modules/foo

It can be used into package.json:

"scripts": {
    "link": "(mkdir node_modules || true) && ln -nsf ../foo node_modules/foo && ln -nsf ../bar node_modules/bar",
    "setup": "yarn run link && yarn"
  },

Just run setup task:

  $> yarn run setup

@StarpTech
Copy link

Run into the same issue. After linking and install the package I got:

yarn
yarn install v0.23.2
[1/4] Resolving packages...
error Received malformed response from registry for "local-test". The registry may be down.
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.

@bestander
Copy link
Member

We are going to revamp link: feature soon, see yarnpkg/rfcs#34

@voxsim
Copy link
Contributor

voxsim commented Apr 30, 2017

Ok I will wait you guys, if you need some help let me know :)

@gsklee
Copy link
Contributor

gsklee commented Jun 21, 2017

@RusPosevkin could you confirm that if this issue has been resolved in v0.26 prerelease?

@fugufish
Copy link

it does not seem to to work on latest prerelease

@alexre0

This comment has been minimized.

@bestander
Copy link
Member

I could not reproduce on latest yarn@0.27.3.
Closing.

@fugufish and @alexre0, please provide a test script how I can reproduce it not working.
Better open a new issue with the steps.

@fugufish
Copy link

fugufish commented Jul 1, 2017

create a brand new package that is not registered on npm, use yarn link <package-name>
create project that references that package and version, use yarn link <package-name>
package correctly shows up in node_modules
call yarn install
an error will be thrown saying the package does not exist on the npm registry

@bestander
Copy link
Member

first time you need to call yarn link without package name.

@fugufish
Copy link

fugufish commented Jul 1, 2017

either way it doesn't work.

@cloakedninjas
Copy link

This is borked for me too - only when my dependency is installed locally can I get things to work, otherwise:

var gulp = require('gulp');
Error: Cannot find module 'gulp'

@bestander
Copy link
Member

I think we are talking about different issues here.

@fugufish, do I understand it right that you yarn link project-b and also have a project-b in package.json?
I think Yarn may not work with linking and depending at the same time, it's probably a separate bug.
For a workaround you can try "project-b": "link:./path/to/project-b", it will create a symlink when you run yarn install.

@bestander
Copy link
Member

@cloakedninjas, yarn would not run install on linked project, this is expected behavior.
Workspaces feature should address this, it is still experimental

@cobyism
Copy link

cobyism commented Oct 3, 2017

Pretty sure there’s still something messed up with this command. Here’s what I’m running into:

$ cd ~/code/some-module

$ yarn link

yarn link v0.17.8
success Registered "some-module".
info You can now run `yarn link "some-module"` in the projects where you want to use this module and it will be used instead.
✨  Done in 0.07s.

$ cd ../main-project

$ yarn link "some-module"

yarn link v0.17.8
error No registered module found called "some-module".
info Visit https://yarnpkg.com/en/docs/cli/link for documentation about this command.

What am I missing?

@monokrome
Copy link

This is still an issue 😢

@joegasewicz
Copy link

Is anyone working on this issue?

@ghost
Copy link

ghost commented Feb 17, 2021

@bestander What's going on with this issue? There's PR #7028 ready to go with minimal changes and a lot of people are waiting on it. You mentioned that linking will be revamped soon, but that was 2017 and it's now 2021. Can we just get the PR approved and fix this bug?

@gjolund
Copy link

gjolund commented May 11, 2021

The yarn official documentation should link to this ticket so that people know not to waste their time trying to link packages like they have with npm for years.

Update: After a few recommendations to try yalc I gave it a shot and it resolved the issue. Naively symlinking whole directories should not be the default solution to this problem.

@hunter2009
Copy link

This is still an issue ! Has any plan to fix it?

@Karl-EdwardFPJeanMehu
Copy link

It seems as if this may never be worked on....'16 to '21 c'mon!

@StoneYaoTreelab
Copy link

Creating the link with sudo works for me.

@MarsianMan
Copy link

For me (with Yarn 2) the link seems to work, but unlink does not.

$ yarn unlink ../../module
Usage Error: Couldn't find a script named "unlink".

@brandongit2

This comment has been minimized.

@merceyz
Copy link
Member

merceyz commented Jul 25, 2021

@MarsianMan The yarn unlink command was added in 3.0.0-rc.3, unfortunately the docs reflect master and not the latest stable release.

@qmolab

This comment has been minimized.

@monokrome
Copy link

monokrome commented Oct 18, 2021

This is still an issue and it apparently will never be fixed. Just use npm...

This is the unfortunate truth that I think most people in this thread likely landed on. This issue has been open for 5 years now and is still a major problem. The time to move on was long ago.

People marking that as spam show the lack of interest in actually improving yarn. Instead of reading it as "hey, this bug is 5 years old now - maybe consider prioritizing major broken features" they decided to read it as simply "just use npm" even though that is literally the only practical solution at this point.

@adameldefrawy
Copy link

Maybe just delete the yarn link from the documentation if it doesn't work!

@jlapier
Copy link

jlapier commented Nov 30, 2021

I was trying to use yarn link (yarn 3.0.2) to link to a local folder and was getting an error about workspaces:

# from another package, e.g. ~/devel/something-api
❯ yarn link ../something-model
Usage Error: The nearest package directory (/Users/jlapier/devel/something-model) doesn't seem to be part of the project declared in /Users/jlapier.

- If the project directory is right, it might be that you forgot to list devel/something-model as a workspace.
- If it isn't, it's likely because you have a yarn.lock or package.json file there, confusing the project root detection.

$ yarn link [-A,--all] [-p,--private] [-r,--relative] <destination>

This was working for another dev on my team so I suspected it was a global config problem. Those who used sudo successfully were probably just skipping their global config in their user directory (by acting as a different user, root).

For me, what worked was to clear out some user config, specifically I deleted this file and this directory:

~/.yarnrc
~/.config/yarn/link

(I'm sure these were created through all the trial and error of trying to get this to work and reading various blog posts over the years of how this used to work.)

@herleraja
Copy link

I had to downgrade my yarn to 1.22.4 to make it work.

To check yarn version:
yarn -v

To set specific yarn version:
yarn set version 1.22.4

@AgostinhoMayoral
Copy link

AgostinhoMayoral commented Dec 20, 2022

rm -rf node_modules (project-a)
rm -rf yarn.lock (project-a)

rm -rf node_modules (project-b)
rm -rf yarn.lock (project-b)

yarn cache clean -f

yarn (project-a)
yarn (project-b)

yarn link (project-b)
rollup --config config/cjs.js -m --watch (project-b)

node ./scripts/linkPackages.js --link project-b --origin (project-a)

It worked for me

@p-sebastian
Copy link

For anyone struggling yarn 3 on a private package

on your package

yarn link

Then on the repo you want the linked package

# p is for private package 
yarn link -p ~/path/to/my/package

This will add this line to the resolutions on the package.json

"package": "portal:/absolute/path/to/my/package"

@ylkhayat
Copy link

ylkhayat commented Apr 19, 2023

@p-sebastian but did that install it in your project and you were able to import it and moreover use it?

Update for Yarn 3:

So what resolutions does as far as I know is strict the packages listed within the resolutions object to the version applied and neglect any implied other versions from any other packages.

Let me illustrate what I mean:

package.json

{
  "dependencies": {
    "some-package": "0.1" // uses "lodash" version 0.2.0
  },
  "resolutions": {
    "lodash": "0.2.1" // stricts the use of "lodash" to version 0.2.1
  }
}

Assuming some-package uses lodash, but uses lodash version 0.2.0. Using resolutions here strictly tells yarn not to install anything but the mentioned version, which is 0.2.1 even if 0.2.0 is inferred. Which is dangerous if you think about it because maybe there's an incompatibility. However back to the topic.

I could use the previously linked private package in my main project through @p-sebastian suggestion and additionally adding my private package to the dependencies with the following command

yarn add <package-name>

Thus, I am not sure if I am missing something or not but from my explanation to the resolutions and adding the package to my dependencies, this means that this will only work if I have my package published on npm and such. Maybe this can be done through a yarn link with different arguments, I am not sure.

@steinybot
Copy link

yarn link is totally unfit for the purpose of testing dependent packages before publishing to a repository. Even if you do get yarn link to work (it does for me) yarn install will try and resolve it again if it is listed in the package.json with a specific version, even if the versions match.

This is such a common use case that every other package manager solves. In the JVM world there has been local publishing of snapshots for several decades.

You may as well give up on yarn link and go through the headache of messing up your package.json and use
yarn add link:/path/to/local/folder instead.

@MiroslavPetrik
Copy link

Outstanding this does not work even in v4

I'm running yarn link @form-atoms/field in package @form-atoms/flowbite.

Usage Error: Invalid destination '@form-atoms/field'; Can't link the project to itself

@atomoc
Copy link

atomoc commented Dec 19, 2023

Outstanding this does not work even in v4

I'm running yarn link @form-atoms/field in package @form-atoms/flowbite.

Usage Error: Invalid destination '@form-atoms/field'; Can't link the project to itself

similarly

@justtal
Copy link

justtal commented Dec 21, 2023

Outstanding this does not work even in v4
I'm running yarn link @form-atoms/field in package @form-atoms/flowbite.
Usage Error: Invalid destination '@form-atoms/field'; Can't link the project to itself

similarly

Same here... :\

@devneill
Copy link

devneill commented Jan 25, 2024

@justtal @atomoc @MiroslavPetrik

Try this for Yarn 4 - it worked for me
#6880 (comment)

@Poolshark
Copy link

Poolshark commented Jan 26, 2024

For everyone using Yarn @3.6.3 (Yarn 3) and workspaces. This worked for me:

Assume the following folder structure:

project/
  package1/
    package.json
    ...
  package2/
    package.json
    ...
 package.json
  1. Link your repo you want to link yarn link
  2. Navigate to the project you want to use your local package (eg. cd ../../projects/project)
  3. Type yarn link -p <relative-path-to-your-local-package>
  4. Check the package.json in the project root → you should see
"resolutions": {
    "your-package-name": "portal:/absolute-path-to-your-package"
  }
  1. Add the package to the workspace dependencies (or dev-dependencies)
"devDependencies": {
  "your-package-name": "*",
  ...
}

This worked for me!

@yojeek
Copy link

yojeek commented Feb 22, 2024

Same issue.

~/workspace/posenet/burns-audio-wam
❯ yarn link @webaudiomodules/sdk-parammgr
Usage Error: Invalid destination; Can't link the project to itself
❯ yarn -v
3.1.1

@callaginn
Copy link

@Poolshark Running yarn link -p works, but it adds a path to our local user folders in package.json. This may be an issue for a lot of people who are sharing package.json across multiple machines.

Is there a way to do that without adding links to your home directory in package.json?

@Poolshark
Copy link

Poolshark commented Feb 23, 2024

@callaginn I think I do not quite understand. Why would you share the target repository with a linked local package? Since the package only exists locally, you would need to share the local package source too.

I'm not certain about your use-case but usually the only reason for yarn link is to test a package locally before deploying it to NPM.

In any other case I would make the package part of the yarn workspace and thus it will be available to everyone who has access to the repository.

@pepijn-vanvlaanderen
Copy link

Just use npm link, it is compatible with yarn and works out of the box.

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

No branches or pull requests