yarn icon indicating copy to clipboard operation
yarn copied to clipboard

Yarn link does not work

Open ApolloTang opened this issue 6 years ago • 31 comments

This is a bug report

Current behavior, and step to produce:

To illustrate the problem (bug) I have created a sample repo, which you can find at: https://github.com/ApolloTang/issue-yarn-link-does-not-work

It has two folders: one has content which is the dependency package, the other a project that using the dependency package:

~/1/1-a/16-tools/yarn-link/issue-yarn-link-does-not-work
$ tree -F
.
├── dependency/
│   ├── index.js
│   ├── node_modules/
│   ├── package.json
│   └── yarn.lock
└── using-dependency/
    ├── index.js
    └── package.json

in dependency/ folder:

$ yarn link

screenshot:

screen shot 2019-01-06 at 1 37 07 pm

This created a link in my ~/.config/yarn/link. To confirm this :

cd ~/.config/yarn/link; ls -l 

screenshot:

screen shot 2019-01-06 at 1 26 54 pm

My ~/.config/yarn has the following structure:

screenshot: screen shot 2019-01-06 at 2 04 47 pm

From the above the link is indeed pointing to my dependency folder. To confirm:

screenshot:

screen shot 2019-01-06 at 1 31 24 pm

Now that the link has created I can cd to the project folder to link the dependency; However, I got

error No registered package found called ....

screenshot:

screen shot 2019-01-06 at 1 41 38 pm

I also notice that if I go back to the dependency package folder, yarn can not unlink the package:

screenshot:

screen shot 2019-01-06 at 1 50 52 pm

It seem like yarn link does not know where the directory~/.config/yarn/link is.

Here is the output of my yarn config list:

screen shot 2019-01-06 at 2 01 06 pm

Here is the content of my .yarnrc:

screen shot 2019-01-06 at 2 00 11 pm

Here is my environment:

screenshot: screen shot 2019-01-06 at 2 10 51 pm

ApolloTang avatar Jan 06 '19 19:01 ApolloTang

duplicate of #1297 as "re-reported" by @cobyism, @fugufish, @JimmyCDChen and other.

  • tried yarn link dependency (without quote) does not work [suggested by @travomic]
  • tried yarn build in dependency folder does not work, there is no such command yarn build [suggested by @sandalsoft]
  • using sudo does not work [suggested by @ConAntonakos]

ApolloTang avatar Jan 07 '19 01:01 ApolloTang

the symbolic link is created in .config/yarn/link/ after all, but yarn link command could not find it, could it be related to #2334, since I am using .config (XDG specification)?

ApolloTang avatar Jan 07 '19 01:01 ApolloTang

probably related: #6801

ApolloTang avatar Jan 10 '19 08:01 ApolloTang

This issue also started happening for me. On yarn version 1.12.3

loudmouth avatar Jan 10 '19 08:01 loudmouth

For me, it looks like the path that is set for the symbolic link is the absolute path prefixed with '../''s. Once this is fixed, yarn link works.

alexseitsinger avatar Jul 10 '19 18:07 alexseitsinger

Jan 18 2020 - still happening.

ls -l symlink says ../../../../../../absolute/path/here

What a bother.

anitagraham avatar Jan 18 '20 02:01 anitagraham

The same problem, can not resolve the module even if the link is created. version 1.21.1

jaizz2003 avatar Feb 04 '20 11:02 jaizz2003

I had the same problem. Only that, it was working few days ago. Later, when I changed the directory of my project, it stopped working. I tried linking again and it showed linked successfuly. Still, it throws a module not found error.

harshil-darji avatar Feb 12 '20 07:02 harshil-darji

Same problem using lerna.

hiukky avatar Jun 01 '20 15:06 hiukky

For a temporary solution, you can direct link by providing direct paths wherever you import that particular module and then rebuild the whole project.

import​ ​ CornerstoneViewport​ ​ from
'../../../react-cornerstone-viewport-2.1.0/src/CornerstoneViewport/Corn
erstoneViewport'​ ;

jaizz2003 avatar Jun 02 '20 01:06 jaizz2003

For a temporary solution, you can direct link by providing direct paths wherever you import that particular module and then rebuild the whole project.

import​ ​ CornerstoneViewport​ ​ from
'../../../react-cornerstone-viewport-2.1.0/src/CornerstoneViewport/Corn
erstoneViewport'​ ;

One other solution ( works but not always) Add the repository name in the projects node modules folder ( directly along with the version) and rebuild.

A temporary solution I thought was to use file:path in package.json ... could be a better solution and avoid the various paths in the imports

hiukky avatar Jun 02 '20 02:06 hiukky

It seems I found why it is not working as expect. My ~/.config is a symlink, so the the symlink which yarn generate is totally wrong in a symlink folder. After making ~/.config to a real folder, everything works well!

linkdesu avatar Jun 21 '20 06:06 linkdesu

yes, my .config is a symlink !

ApolloTang avatar Jun 23 '20 20:06 ApolloTang

I have a 'real' ~/.config dir, and yarn link is not working correctly.

Only I get no error messages. Or any other messages. Screen Shot 2020-07-02 at 11 48 58 AM

(I had to use Activity Monitor to SIGKILL node, as ctrl+c was leaving the webpack-dev-server up, because reasons, I assume.)

The ui-kit (celula) builds just fine, and todos builds just fine without the kit.

igreulich avatar Jul 02 '20 15:07 igreulich

I have a similar problem with yarn link, I'm linking my own library framework in my project client

This is the error message: Could not load /Users/x/framework/node_modules/@babel/runtime/helpers/interopRequireDefault (imported by ../framework/dist/index.js): ENOENT: no such file or directory, open '/Users/x/framework/node_modules/@babel/runtime/helpers/interopRequireDefault'

The file /Users/x/project/node_modules/@babel/runtime/helpers/interopRequireDefault exists.

The compiled code by babel that triggers the above error is this line:

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

when I add the file extension, the error disappears:

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault.js");

I have the same problem if link it with npm and I haven't found the way to configure babel so that it includes the file extension. The solution was to link it through the package.json like this:

"dependencies": {
  "framework": "link:../framework"
}

simoncarbajal avatar Dec 11 '20 19:12 simoncarbajal

I also encountered this issue with 1.22.10 where my ~/.config is a symlink and although a symlink is created in ~/.config/yarn/link I am unable to register it (or unlink it). I symlink my ~/.config to manage my dotfiles across development machines (I suspect this is the use case others are doing as well). Related issues: https://github.com/yarnpkg/yarn/issues/7614, https://github.com/yarnpkg/yarn/issues/7388, https://github.com/yarnpkg/yarn/issues/7377

bdefore avatar Jun 30 '21 17:06 bdefore

Just figured out link is applied on yarn add command only. If you already have the remote package installed and you link it to a local one, you won't have the local one installed even after removing yarn.lock etc.

The only solution for me is to yarn remove the package and yarn add again.

existe-deja avatar Nov 29 '21 12:11 existe-deja

Just figured out link is applied on yarn add command only. If you already have the remote package installed and you link it to a local one, you won't have the local one installed even after removing yarn.lock etc.

The only solution for me is to yarn remove the package and yarn add again.

@existe-deja this is exactly what I needed. Thank you!

jacobsfletch avatar Sep 09 '22 02:09 jacobsfletch

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"

p-sebastian avatar Feb 24 '23 11:02 p-sebastian

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"

im getting

$ yarn link Unknown Syntax Error: Not enough positional arguments.

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

any help

alikazai avatar Jul 01 '23 01:07 alikazai

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"

This very much didn't work for me. The yarn link -p command tried somehow to link the repo I was in (where I wanted to use my linked package)

➜  api_v5 ✗ yarn link
yarn link v1.22.19
success Registered "@myorg/api-v5-typescript-schema".
info You can now run `yarn link "@ myorg/api-v5-typescript-schema"` in the projects where you want to use this package and it will be used instead.
✨  Done in 0.06s.
➜  api_v5 ✗ cd ../storefront-remix
➜  storefront-remix git:(develop) ✗ yarn link -p ~/dev/myorg/myorg-api/typescript-schemas/api_v5
yarn link v1.22.19
success Registered "platform".
info You can now run `yarn link "platform"` in the projects where you want to use this package and it will be used instead.
✨  Done in 0.04s.

tuzmusic avatar Aug 04 '23 20:08 tuzmusic

Is anyone still struggling with this? I have a very simple setup with two local packages one linked and the other should install the first one from local but I keep getting 404 errors, I tried removing and adding again but that did nothing

isaacfink avatar Aug 31 '23 07:08 isaacfink

Just been bitten by this as well, and this issue helped tremendously so thank you 🙇🏽

Using yarn 1.22.19 and my ~/.config folder was a symlink, so this seems to be a good root cause.

Shahor avatar Sep 06 '23 07:09 Shahor

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"

im getting

$ yarn link Unknown Syntax Error: Not enough positional arguments.

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

any help

Hi, not sure if you still need help. But if you don't this may also help other people.

I've been struggling to link my local project all day and finally come to the conclusion that yarn link is aimless for Yarn 3+ at least. This stack overflow answer helped, you need to use the portal protocol to obtain the package which is the symlink. Okay, cool, but how do we use it?

The only solution that worked for me is to:

yarn add package-name@portal:<path to package name>.

I've tested this over and over again, and doing a yarn link on the package simply doesn't work. You need to add the symlink via a portal using the above yarn add.

If you somehow figure out that yarn link does indeed work for you (and you're using Yarn 3+) you cannot set a link on the current package but rather in the place you require it, you need to link it from there. https://yarnpkg.com/cli/link

Edit: For further help to those still a little confused. Ensure the package you want to import has been built. If you're using a front-end tool like Vite, you can use the command yarn build --watch to continuously build the package on every save and the package using the imported package will update.

Hope this helps all :)

norvalbv avatar Sep 16 '23 21:09 norvalbv

I am hitting this with [email protected].

When looking in my .config/yarn/link folder, the symlink was there but was invalid. I deleted it and manually created the symlink myself, and now I can successfully link this package into another package that wants to use it.

my .config folder is not a symlink.

Manually creating the symlink is tedious, but it does accomplish the task so I'll take it.

city41 avatar Oct 06 '23 20:10 city41

I am hitting this with [email protected].

When looking in my .config/yarn/link folder, the symlink was there but was invalid. I deleted it and manually created the symlink myself, and now I can successfully link this package into another package that wants to use it.

my .config folder is not a symlink.

Manually creating the symlink is tedious, but it does accomplish the task so I'll take it.

Where's the .config/yarn/link folder located?

alexander01202 avatar Oct 22 '23 18:10 alexander01202

@alexander01202

I am hitting this with [email protected]. When looking in my .config/yarn/link folder, the symlink was there but was invalid. I deleted it and manually created the symlink myself, and now I can successfully link this package into another package that wants to use it. my .config folder is not a symlink. Manually creating the symlink is tedious, but it does accomplish the task so I'll take it.

Where's the .config/yarn/link folder located?

Your home directory:

$ echo $HOME

ApolloTang avatar Oct 22 '23 20:10 ApolloTang

It seems for yarn 4 the way link works has changed from older versions. I was confused, but I tried the method from the yarn 4 docs and it seems to work.

Yarn classic

  1. Register your package with yarn link form the package directory
  2. Link to the registered package by running yarn link "package name" from the consuming app directory

Yarn 4

  1. Link to your package by running yarn link 'relative/path/to/your/package' from the consuming app directory

devneill avatar Jan 25 '24 12:01 devneill

@p-sebastian Running yarn link -p ~/path/to/my/package works, but there's a breaking issue with this approach. Since we're using multiple machines with these installs, we don't need yarn to insert paths to our individual home directories in package.json. Otherwise, those changes constantly get synched via git and have to be changed every time we do a git pull.

Is there a way to link to global packages without having to insert your home directory into the package.json resolutions? We're trying to globally link to some pretty large libraries like Font Awesome, so we can package them into custom fonts per site.

This used to be possible with npm global installs, but newer versions of yarn seem to be deprecating all this.

callaginn avatar Feb 23 '24 18:02 callaginn

yarn 4+ couldn't use yarn link in projectA to be able to link it in projectB using yarn link projectA or a relative path yarn link rel/projectA

projectA is using yarn 4+ but projectB is using yarn classic

EDIT: upgrading yarn in projectB (yarn set version berry) solved my issue now using yarn link rel/path/to/projectA inside projectB works fine

mp3846 avatar Apr 19 '24 11:04 mp3846