Skip to content

Instantly share code, notes, and snippets.

@osde8info
Forked from json2d/deptree-json-sync.js
Created May 14, 2019 15:08
Show Gist options
  • Save osde8info/554e89b0d4474a9290cfa8ada3ed1976 to your computer and use it in GitHub Desktop.
Save osde8info/554e89b0d4474a9290cfa8ada3ed1976 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