How do I get a list of all globally installed npm packages using the npm.commands.ls
command?
1 Answer
First, you have to have npm installed locally(npm i npm -S
). Second, you need to do npm.load
to initialize npm. Finally you can just use npm.commands.ls
to get the list. What is important is to add the global: true
and depth: 0
parameters into the load function.
var npm = require('npm')
npm.load({
loaded: false,
progress: false,
loglevel: 'error',
global: true,
depth: 0
}, () => {
npm.commands.ls([], true, (err, data) => {
console.log(Object.keys(data.dependencies))
})
})