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 does not work #6880

Open
ApolloTang opened this issue Jan 6, 2019 · 32 comments
Open

Yarn link does not work #6880

ApolloTang opened this issue Jan 6, 2019 · 32 comments
Assignees
Labels

Comments

@ApolloTang
Copy link

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

@ghost ghost assigned arcanis Jan 6, 2019
@ghost ghost added the triaged label Jan 6, 2019
@ApolloTang
Copy link
Author

ApolloTang commented Jan 7, 2019

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
Copy link
Author

ApolloTang commented Jan 7, 2019

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
Copy link
Author

probably related: #6801

@loudmouth
Copy link

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

@alexseitsinger
Copy link

alexseitsinger commented Jul 10, 2019

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.

@anitagraham
Copy link

Jan 18 2020 - still happening.

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

What a bother.

@jaizz2003
Copy link

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

@harshil-darji
Copy link

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.

@hiukky
Copy link

hiukky commented Jun 1, 2020

Same problem using lerna.

@jaizz2003
Copy link

jaizz2003 commented Jun 2, 2020

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'​ ;

@hiukky
Copy link

hiukky commented Jun 2, 2020

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

@linkdesu
Copy link

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!

@ApolloTang
Copy link
Author

yes, my .config is a symlink !

@igreulich
Copy link

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.

@simoncarbajal
Copy link

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"
}

@bdefore
Copy link

bdefore commented Jun 30, 2021

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: #7614, #7388, #7377

@existe-deja
Copy link

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.

@jacobsfletch
Copy link

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!

@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"

@alikazai
Copy link

alikazai commented Jul 1, 2023

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

@tuzmusic
Copy link

tuzmusic commented Aug 4, 2023

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.

@isaacfink
Copy link

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

@Shahor
Copy link

Shahor commented Sep 6, 2023

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.

@norvalbv
Copy link

norvalbv commented Sep 16, 2023

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 :)

@city41
Copy link

city41 commented Oct 6, 2023

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.

@alexander01202
Copy link

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?

@ApolloTang
Copy link
Author

@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

@devneill
Copy link

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

@callaginn
Copy link

callaginn commented Feb 23, 2024

@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.

@mp3846
Copy link

mp3846 commented Apr 19, 2024

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

@fmaiabatista
Copy link

fmaiabatista commented Sep 26, 2024

After attempting some of the suggestions here but still struggling to get it to work, I somehow managed it:

  • Removed my-local-package from my-project: yarn remove my-local-package.
    • Now the package is not present in package.json, rather there are only "orphan" imports across component files.
  • Linked my-local-package from the root of my-local-package: yarn link
  • Applied the link on the project from the root of my-project: yarn link my-local-package

The above made it work for me. In addition, every time I build a new version of my-local-package with yarn dist (which in my package is npm run clean-dist && rollup -c), the dev server of my-project picks up the changes.

Sharing this just in case, as it seems a lot of people struggled with the other suggestions too.

@szekelyisz
Copy link

I used linking routinely for yarn versions 1 and 4, I know how it works. Suddenly it stopped working for version 1. Tried everything above but nothing worked.

It's disappointing to see this bug open for more than 6 years.

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