Skip to content

Instantly share code, notes, and snippets.

@json2d
Created August 4, 2017 16:10
Show Gist options
  • Save json2d/2cf1aec242417662632c937685983455 to your computer and use it in GitHub Desktop.
Save json2d/2cf1aec242417662632c937685983455 to your computer and use it in GitHub Desktop.
Walk through dependency tree of installed packages via `npm ls`
const spawn = require('cross-spawn');
const path = require('path')
const ls = spawn.sync('npm', ['ls','--json']);
const tree = JSON.parse(ls.stdout)
const walkDepTree = (tree,cb) => {
const deps = tree.dependencies
if(deps) {
Object.keys(deps).forEach(name => {
const dep = deps[name];
cb(name,dep)
walkDepTree(dep,cb);
})
}
}
walkDepTree(
tree,
(name,data)=> {
if(data.resolved.startsWith('https://registry.npmjs.org/'))
console.log(`${name} @ ${data.resolved}`)
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment