Skip to content

Instantly share code, notes, and snippets.

@dankahle
Last active August 29, 2015 14:18
Show Gist options
  • Save dankahle/d4dc20c3bead6a1d1a41 to your computer and use it in GitHub Desktop.
Save dankahle/d4dc20c3bead6a1d1a41 to your computer and use it in GitHub Desktop.
Read node module versions for inclusion into package.json

I stole this off a stackoverflow answer, it reads the packages and output "packagename": "version" pairs to the console. "npm init" doesn't have an option do prefill these versions like bower does. I suppose that's because bower doesn't have dev/prod/peer dependencies to contend with. Either way, this gets the info where you can cut and paste it to the correct section.

var fs = require("fs");
function main() {
fs.readdir("./node_modules", function (err, dirs) {
if (err) {
console.log(err);
return;
}
dirs.forEach(function(dir){
if (dir.indexOf(".") !== 0) {
var packageJsonFile = "./node_modules/" + dir + "/package.json";
if (fs.existsSync(packageJsonFile)) {
fs.readFile(packageJsonFile, function (err, data) {
if (err) {
console.log(err);
}
else {
var json = JSON.parse(data);
console.log('"'+json.name+'": "' + json.version + '",');
}
});
}
}
});
});
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment