Print a list of all installed node.js modules

184,726

Solution 1

Use npm ls (there is even json output)

From the script:

test.js:

function npmls(cb) {
  require('child_process').exec('npm ls --json', function(err, stdout, stderr) {
    if (err) return cb(err)
    cb(null, JSON.parse(stdout));
  });
}
npmls(console.log);

run:

> node test.js
null { name: 'x11', version: '0.0.11' }

Solution 2

If you are only interested in the packages installed globally without the full TREE then:

npm -g ls --depth=0

or locally (omit -g) :

npm ls --depth=0

Solution 3

list of all globally installed third party modules, write in console:

 npm -g ls

Solution 4

in any os

npm -g list

and thats it

Solution 5

Generally, there are two ways to list out installed packages - through the Command Line Interface (CLI) or in your application using the API.

Both commands will print to stdout all the versions of packages that are installed, as well as their dependencies, in a tree-structure.


CLI

npm list

Use the -g (global) flag to list out all globally-installed packages. Use the --depth=0 flag to list out only the top packages and not their dependencies.


API

In your case, you want to run this within your script, so you'd need to use the API. From the docs:

npm.commands.ls(args, [silent,] callback)

In addition to printing to stdout, the data will also be passed into the callback.

Share:
184,726
Anderson Green
Author by

Anderson Green

I write source-to-source compilers in JavaScript using Peggyjs. I also write compilers in Prolog. For reference, I also have a list of source-to-source compilers on GitHub.

Updated on July 08, 2022

Comments

  • Anderson Green
    Anderson Green almost 2 years

    In a node.js script that I'm working on, I want to print all node.js modules (installed using npm) to the command line. How can I do this?

    console.log(__filename);
    
    //now I want to print all installed modules to the command line. How can I do this?
    
  • Anderson Green
    Anderson Green over 11 years
    Also, how can you obtain the file path of the modules folder?
  • Andrey Sidorov
    Andrey Sidorov over 11 years
    path would be node_modules/[module name]. I believe this should work on all platforms. Note that this way only 'local' modules tree is printed, and requre looks first at node_modules, then ../node_modules, ../../node_modules ( see nodejs.org/api/… ) and then from NODE_PATH env var
  • Andrey Sidorov
    Andrey Sidorov over 11 years
    try npm ls --parseable for just list of paths
  • Adam Caviness
    Adam Caviness about 9 years
    I like specifying --l or --long as well, npm -g ls --depth=0 --long. This provides the module descriptions and github links.
  • A T
    A T about 8 years
    Yeah, all my Bash nowadays has $()
  • prosti
    prosti over 7 years
    if you like npm ls full examples check this out: stackoverflow.com/questions/17937960/…
  • Owen J Lamb
    Owen J Lamb over 7 years
    That won't give you packages installed globally
  • Andy Fleming
    Andy Fleming over 6 years
    The original question doesn't specify whether or not they want to include globally installed packages. This answer provides a helpful alternative to the other answers.
  • Paul Razvan Berg
    Paul Razvan Berg almost 6 years
    This command will take longer than npm -g ls --depth=0, because it will also look for module dependencies.
  • Giuliano Collacchioni
    Giuliano Collacchioni almost 4 years
    Thanks for specifying that npm has an API accessible from applications. How do you pass arguments to the functions? I tried npm.commands.ls(["depth=0"], ... ) but it gives me error and npm.commands.ls(["prod"], ... ) gives me an empty array....