219

I am looking for a command that will list the names of global modules that I have npm link'd to local copies, also listing the local path.

In fact, a list of all globally installed modules would be even better, with the npm link'd ones flagged somehow.

3
  • 3
    npm -g ls should list all global modules, but I don't know if it lists linked modules
    – Ferdi265
    Commented Jul 24, 2014 at 12:42
  • maintain a "package.json" file for your application
    – C M
    Commented Jul 24, 2014 at 13:36
  • 1
    @CeeAim I have lots of applications with lots of package.jsons, I also have quite a few global modules for other purposes like CLIs, and many are forked and npm link'd. I need a way to keep track.
    – callum
    Commented Jul 24, 2014 at 13:42

10 Answers 10

387

To list all globally linked modules, this works (documentation https://docs.npmjs.com/cli/ls):

npm ls -g --depth=0 --link=true

I had to update the version of npm on my machine first, though:

npm install npm@latest -g
4
  • The 1st one works w/o --link=true. My environment: Win10, NVM=1.1.7, current Node= 8.14.0
    – Jeb50
    Commented Jan 18, 2019 at 4:53
  • 14
    and to remove one that's listed: npm unlink <package> -g
    – zamnuts
    Commented Apr 12, 2020 at 3:19
  • @Jeb50 that makes sense, because linked packages are installed globally.
    – kas
    Commented May 27, 2020 at 1:32
  • 7
    With newer npm versions you can shorten this to npm ls -g --link Commented Sep 16, 2021 at 0:04
73

Did you try just listing the node_modules directory contents (e.g., ls -l node_modules | grep ^l)? They're normal symbolic links.

If you really need to find all symbolic links, you could try something like find / -type d -name "node_modules" 2>/dev/null | xargs -I{} find {} -type l -maxdepth 1 | xargs ls -l.

4
  • 21
    Doesn't work with locally linked namespaced modules (@namespace/moduleName). A brutal solution might be: ( ls -l node_modules ; ls -l node_modules/@* ) | grep ^l Commented Apr 27, 2016 at 11:04
  • 2
    This solution doesn't work on Windows (nor do any other solutions). Commented Mar 4, 2017 at 3:09
  • 3
    @MicahZoltu It will work on Windows if you use the Git bash terminal
    – Greg M.
    Commented Jun 22, 2017 at 19:46
  • 3
    For @scoped packages, just add -R to the ls command: ls -l -R ./node_modules | grep ^l
    – Romasato
    Commented Jul 11, 2018 at 16:03
48

This command is simpler as of npm 7:

npm ls --link --global

Credit to Andrew for finding the --link flag

4
  • 1
    When was it released? Commented Oct 22, 2021 at 16:49
  • October 12th, 2020 Commented Oct 22, 2021 at 20:07
  • Looks like it might now be --location=global
    – dras
    Commented Oct 4, 2022 at 12:35
  • Both seem to work with npm 8. Is there a deprecation notice somewhere? Commented Oct 19, 2022 at 10:14
24

A better alternative to parsing ls is to use find like this:

find . -type l

You can use -maxdepth 1 to only process the first directory level:

find . -maxdepth 1 -type l

You can use -ls for additional information.

For instance, for finding Node.js modules that are npm linked:

find node_modules -maxdepth 1 -type l -ls

Here's an article why parsing ls is not the best idea.

1
14

If you want a nice colored output from npm list, you may like:

\ls -F node_modules | sed -n 's/@$//p' | xargs npm ls -g --depth 0

which gives in my current playground directory:

+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
`-- [email protected]

It makes a few assumptions, but it should work in most cases, or be easy to adapt with the explanations below.

  • use \ls to bypass possible aliases on your ls command
  • the -F option adds an '@' indicator for links
  • the sed command selects those links and removes the indicator
  • the xargs part passes previous output as arguments to npm ...
  • npm is invoked with
    • list or ls to list modules with versions
    • replace with ll to get details about each listed module.
    • -g for the global modules and
    • --depth 0 for a shallow listing (optional)
    • --long false (default with 'list').

Issue: for some reason npm gives extraneous entries for me at the moment (non colored). They would be those I had "npm unlink"ed.

For "a list of all globally installed modules" in current npm path, you just do

npm list -g

For further needs you may want to have a look at

npm help folders

You cannot follow symlinks backwards unless you scan your whole filesystem and (then that's not a npm specific question).

For quickly finding files and directories by name, I use locate which works on an index rebuilt usually once a day.

locate '*/node_modules'

and start working from there (you may want to refine the search with --regexp option.

1
  • I use without -g to get my local/nearest transient dependency links. Thanks!
    – kross
    Commented May 2, 2016 at 15:08
5

Use

find `npm root -g` -maxdepth 2 -type l

to show global links, including namespaced packages.

Andrew's answer works some of the time:

npm ls -g --depth=0 --link=true

But it blew up on peer dependency errors for me on some occasions.

4

I made a Node.js module, symlinked, that uses fs to check for symbolic links made by npm link or otherwise.

var symlinked = require("symlinked")

console.log(symlinked.names())
5
  • 2
    Would be really sweet if you added a CLI executable for this, especially in light of medium.com/@maybekatz/…. Most use cases for needing to find symlinked packages are from CLI. Commented Jul 12, 2017 at 21:04
  • couldn't detect any links for some reason :(
    – JacopKane
    Commented Nov 15, 2017 at 10:50
  • 1
    @cchamberlain can you point me to good CLI tool to model the API after? Or definitely feel free to contribute a cli command to github.com/ryanve/symlinked :)
    – ryanve
    Commented Nov 15, 2017 at 18:32
  • @JacopKane Are you able to post an issue on github.com/ryanve/symlinked/issues/new with details?
    – ryanve
    Commented Nov 15, 2017 at 18:33
  • 2
    @ryanve - done github.com/ryanve/symlinked/pull/1 - I also added scoped packages support since that wasn't working. Kudos on the simple structure! :) Commented Nov 16, 2017 at 21:44
4

I found this question after I also wrote my own tool, and here it is for completeness: npm-list-linked.

It will recursively follow all linked packages down in the hierarchy as well. At my work we sometimes may have npm link 2-3 levels deep and this way you can see exactly which are local and which ones are not. It avoids surprises.

npm-list-linked

Output:

Linked packages in /home/user/projects/some-project/
    @prefix/package 0.2.7
        other-package 0.1.2
1

I see myself and others having this same question a lot. I wrote a small CLI for myself called link-status to display this info, and it may help others out too! Check it out here!

0
0

On Windows you can just look at the directory:

C:\Users\[username]\AppData\Roaming\npm\node_modules

You should see any of the symbolic linked libraries listed there, along side any global library installs.

Not the answer you're looking for? Browse other questions tagged or ask your own question.