Skip to content

Instantly share code, notes, and snippets.

@youurayy
Created January 30, 2012 15:30
Show Gist options
  • Save youurayy/1704980 to your computer and use it in GitHub Desktop.
Save youurayy/1704980 to your computer and use it in GitHub Desktop.
Script to search and print top-level module versions for NPM's package.json file (dependencies). Run in project root.
var fs = require('fs');
var res = {};
var base = process.cwd() + '/node_modules';
var dirs = fs.readdirSync(base);
for(var i = 0; i < dirs.length; i++) {
var module = dirs[i];
var bdir = base + '/' + module;
var cfg = bdir + '/package.json';
var ok;
try { ok = fs.lstatSync(cfg).isFile(); } catch(e) {}
if(!ok) {
console.log('skipping: ' + bdir);
continue;
}
try {
var config = JSON.parse(fs.readFileSync(cfg));
if(config.version)
res[module] = config.version;
else
console.log('No version information for module ' + module);
}
catch(e) {
console.log('error during parsing: ' + cfg, e.stack);
process.exit(1);
}
}
console.log(JSON.stringify(res, null, '\t'));
@youurayy
Copy link
Author

Requested to be added to NPM core: https://github.com/isaacs/npm/issues/2105

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment