How to list all the Node.js modules I have linked with npm

node.jsNpm

node.js Problem Overview


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.

node.js Solutions


Solution 1 - node.js

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

Solution 2 - node.js

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.

Solution 3 - node.js

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.

Solution 4 - node.js

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:

+-- color@0.11.1
+-- grunt@0.4.5
+-- http-server@0.8.5
+-- jsdom@8.0.2
+-- jsonfile@2.2.3
+-- underscore@1.8.3
+-- xmlserializer@0.3.3
`-- zombie@4.2.1

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.

Solution 5 - node.js

These commands are simpler as of npm 7:

  • Global modules: npm ls --link --global
  • Local modules: npm ls --link

Credit to Andrew for finding the --link flag

Solution 6 - node.js

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

Solution 7 - node.js

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

Solution 8 - node.js

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.

Solution 9 - node.js

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!

Solution 10 - node.js

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncallumView Question on Stackoverflow
Solution 1 - node.jsAndrewView Answer on Stackoverflow
Solution 2 - node.jsmscdexView Answer on Stackoverflow
Solution 3 - node.jsKonstantine KalbazovView Answer on Stackoverflow
Solution 4 - node.jsRockyRoadView Answer on Stackoverflow
Solution 5 - node.jsNick McCurdyView Answer on Stackoverflow
Solution 6 - node.jsryanveView Answer on Stackoverflow
Solution 7 - node.jsStoffeView Answer on Stackoverflow
Solution 8 - node.jsBaronVonKaneHoffenView Answer on Stackoverflow
Solution 9 - node.jsdanmakenoiseView Answer on Stackoverflow
Solution 10 - node.jsJeffryHouserView Answer on Stackoverflow