Skip to content

Instantly share code, notes, and snippets.

@phette23
Created May 25, 2021 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phette23/d7e5ab84a0924333f6908112866f1199 to your computer and use it in GitHub Desktop.
Save phette23/d7e5ab84a0924333f6908112866f1199 to your computer and use it in GitHub Desktop.
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