Print a list of all installed node.js modules
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.
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, 2022Comments
-
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 over 11 yearsAlso, how can you obtain the file path of the modules folder?
-
Andrey Sidorov over 11 yearspath 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 over 11 yearstry
npm ls --parseable
for just list of paths -
Adam Caviness about 9 yearsI like specifying --l or --long as well,
npm -g ls --depth=0 --long
. This provides the module descriptions and github links. -
A T about 8 yearsYeah, all my Bash nowadays has
$()
-
prosti over 7 yearsif you like
npm ls
full examples check this out: stackoverflow.com/questions/17937960/… -
Owen J Lamb over 7 yearsThat won't give you packages installed globally
-
Andy Fleming over 6 yearsThe 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 almost 6 yearsThis command will take longer than
npm -g ls --depth=0
, because it will also look for module dependencies. -
Giuliano Collacchioni almost 4 yearsThanks 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 andnpm.commands.ls(["prod"], ... )
gives me an empty array....