Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

node.js

npm

People also ask

How do I list all node modules?

js modules. To list the modules installed locally in a project, enter the project directory and execute the npm list command, as shown in the example below.


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

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.


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.


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.