Print a list of all installed node.js modules


Question

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?
1
127
12/21/2012 10:58:30 PM

Accepted Answer

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' }
99
11/20/2015 8:31:57 PM

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


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon