Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
list global npm packages that are not linked
#!/usr/bin/env node
const { exec } = require("child_process")
exec('npm ls --global --json', (err, stdout, stderr) => {
if (err) throw err
const deps = JSON.parse(stdout).dependencies
// dependencies hash looks like:
// "linked-pkg": { "version": "1.0.0", "resolved": "file:..." },
// "global-pkg": { "version": "1.0.0" }, ...
// so we skip any package that has a "resolved" property
const output = Object.keys(deps).reduce((total, key) => {
return deps[key].resolved ? total : `${total}${key}\n`
}, '')
process.stdout.write(output)
process.stderr.write(stderr)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment