Yarn: Add command to show linked packages

Created on 7 Nov 2016  ·  50Comments  ·  Source: yarnpkg/yarn

I think it would be useful to have a command that shows currenlty linked packages (using yarn link [package]) in current project.

cat-feature

Most helpful comment

How about something similar to global?

Usage: yarn link [add|ls|remove] [flags]

All 50 comments

How about something similar to global?

Usage: yarn link [add|ls|remove] [flags]

yarn link [add|ls|remove] [flags]

Yes I think that would be very explicit and nice.

Any movement or workarounds on this one?

I found this workaround: ls -l node_modules | grep ^l

@tnrich nice solution, is there a more general one which supports namespace? e.g. node_modules/@myCompany

@bingzheyuan you can try

( ls -l node_modules ; ls -l node_modules/@* ) | grep ^l

(copied from this SO question)

@jlegrone checked, works great, thanks a lot

Is this feature still being considered? @kittens

+1. It would be nice to have this as a builtin, though for now I have a standalone script that I use:

find node_modules node_modules/\@* -depth 1 -type l -print | while read MODULE ; do
    echo "Linked module in use: $MODULE"
done

This is especially helpful on windows, since you really want yarn to tell you what it thinks it's linked to when debugging symlinks on that platform.

just searched to see if this exists...

More important than the case that the comments here provide workarounds for (to me, at least) is the ability to know what yarn links are _available_ -- that is, what directories I have run yarn link in, and so what associations exist between package-names and directories that contain those packages.

One might think that the ~/.yarn-config/link directory would contain the relevant information -- but it's empty.

@MikeTaylor I think you're looking for ~/.config/yarn/link

I _was_! Thank you so much!

(This does slightly leave me wondering why I have .yarn, .yarn-cache, .yarn-config, .yarnrc _and_ .config/yarn in my home directory, but let it pass.)

Not finding that config

PS C:\Users\me> ls ~/.config


    Directory: C:\Users\me\.config


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2018-03-29   5:59 PM                configstore

PS C:\Users\me> ls ~/.yarn-config
ls : Cannot find path 'C:\Users\me\.yarn-config' because it does not exist.

The only fie I find is ~/.yarnrc

@retog if you're using Windows it looks like the directory you're looking for is: C:\Users\<userName>\AppData\Local\Yarn\config\link.

Thanks @TomGault , the links are there.

hope can have this lol for global and local

This would massively help debugging when working on cross repo integration!
(Situation I currently find myself)

Would indeed be nice with a command like yarn link ls to see all links, especially when you don't remember where a link was created from, so you can remove it.

In the meantime, for macOS/Linux, this should work:

ls -la ~/.config/yarn/link/

i make a stupid way for this, can show current project link list

https://www.npmjs.com/package/yarn-list-link

$ npx yarn-list-link
@node-novel\pattern-split
cjk-conv
lazy-cacache
regexp-cjk
uni-string

On my PC running Windows 10, yarn links are here:
~\AppData\Local\Yarn\Data\link\

any updates ?

find node_modules -type l | grep -v .bin and find ~/.config/yarn/link -type l are helpful for finding linked and linkable modules, respectively.

In an attempt to complement @hubgit post, I believe the following would do for Windows using PowerShell:
Get-ChildItem '.\node_modules\.bin\' | Where-Object {$_.LinkType -eq 'SymbolicLink'}

This will be very helpful if provided. Is there any progress?

+1

+1

Alternatively, implement #710.

If I'm running yarn link and there is already a package registered with the same name, why can't yarn print out the already linked package's path? Additionally, yarn could be smarter by unlinking the other package and link the current one (maybe with a -f flag).

+1

+1

On Windows, dir %LOCALAPPDATA%\yarn\data\link will work without needing to know your username or whatever.

+1

# yarn link list
yll () {
    current=$PWD
    for link in $(find $HOME/.config/yarn/link -type l)
    do
        cd $link"/../"$(readlink $link)
        echo $link $'\n==>' $PWD
    done
    cd $current
}

# yarn unlink [package]
yul () {
    current=$PWD
  link=$(find $HOME/.config/yarn/link -type l | grep $1$)
    cd $link"/../"$(readlink $link)
    yarn unlink
    cd $current
}

@torifat 2 and a half years since this issue was opened and was tagged as a new-feature (cat-feature?), and we haven't heard from the yarn team since. Any movement on this? :)

This helped me find the linked packages in my project:

ls -l node_modules | grep ^l

https://stackoverflow.com/questions/24933955/easy-way-to-list-node-modules-i-have-npm-linked

The problem with ls -l node_modules | grep ^l is that it won't show linked _scoped_ packages (sub-directories under an @ directory like @types)
Quick fix is to add the recursive flag R to the ls command. Can't speak for performance since I tested in a relatively small dependency list:

ls -lR node_modules | grep ^l

Here's what I do to list currently linked packages in a project:

Edit: Not OSX Compatible

yllc() {
  find -L node_modules -maxdepth 3 -type d -xtype l | xargs -I{} sh -c "echo {} '->' $(realpath {})"
}

results in:

node_modules/@somname/somepkg -> /home/user/repos/somename-somepkg

Here's what I do to find packages I can link, that includes scoped packages:

yll () {
  linkDir=~/.config/yarn/link
  find $linkDir -maxdepth 2 -type l | xargs realpath -s --relative-to $linkDir
}

results in:

@somename/foo
@othername/bar
somepkg
otherpkg

I can then use fzf like so:

yarn link $(yll | fzf -m)

I've been using these so often I've made them into bash aliases:

alias yarn-linked="find . -type l | grep -v .bin | sed 's/^\.\/node_modules\///'"
alias yarn-unlink-all="yarn-linked | xargs yarn unlink && yarn install --check-files"

Since we are posting solutions and they all seem so complicated here let me help you all out:

alias yarn-linked="find node_modules -type l -maxdepth 3 | grep -v .bin | cut -f2 -d/"

Easy peasy.
Edit: Though looking I really like @Bkucera solution! :clap:
Edit: My solution is also complicated! Darn it, troll fail!
Edit: @Bkucera, your solution is not MacOS compatible :cry:

can someone from yarn or npm look into this? there is clearly big demand, please <3

+1

yll () {
  linkDir=~/.config/yarn/link
  find $linkDir -maxdepth 2 -type l | xargs realpath -s --relative-to $linkDir
}

This worked like a charm. Thanks for sharing!

The solution that worked for me was to just:

  • go to ~/.config/yarn/link folder
  • delete the modules that you want to unlink

I think a good fix would be to add these instructions to the message

Using yarn 1.x in a project, ended up adding a couple of aliases:

$ alias | grep yarn
yarn-linked='find -L $(find node_modules -type l) -type d -prune'
yarn-links='(cd $HOME/.config/yarn/link && find . -type l | cut -c 3-)'

It doesn't work for me. I haven't got folder ~/.config/yarn/link , all I have there is global folder which is empty.
I use Manjaro linux, if that could help..

Is this still considered?

I ended up making a Yarn wrapper that adds this functionality as well as automatically installing @types packages in TypeScript projects: https://github.com/iansu/blarn

It sounds like there are no objections to this functionality being added to yarn, it just needs to be done by someone. Requirements:

  1. Ability to list packages that have been linked into the CURRENT project. In other words, recursively inspect node_modules folder for symbolic links to external (local) packages.

  2. Ability to unlink all packages that have been linked into the CURRENT project.

  3. Ability to list packages that have been registered in the global link registry directory ($HOME/.config/yarn/link on *nix, %LOCALAPPDATA%\yarn\data\link on Windows)

Since yarn link [add|ls|remove] (one of the earlier suggestions) is not strictly backwards-compatible, I propose: yarn link --list. This flag should create a list of BOTH (1) and (3).

In addition, I propose yarn unlink --all as a way to accomplish (2).

I'm working on a PR. I'm not at all familiar with yarn internals, but if successful, I'll link back to this issue when submitting.

Was this page helpful?
0 / 5 - 0 ratings