Skip to content

Instantly share code, notes, and snippets.

@magnetikonline
Last active February 21, 2026 23:16
Show Gist options
  • Select an option

  • Save magnetikonline/fb9246d26433986b475725a318ec38c4 to your computer and use it in GitHub Desktop.

Select an option

Save magnetikonline/fb9246d26433986b475725a318ec38c4 to your computer and use it in GitHub Desktop.
Generate `npm install` commands from existing package.json
const fs = require('fs');
function buildCommand(deps,flag = '') {
if (!deps) return;
const packages = Object.entries(deps)
.map(([name,version]) => `${name}@${version.replace(/^[\^~]/,'')}`)
.join(' ');
if (flag) {
flag = ' ' + flag;
}
return `npm install${flag} ${packages}`;
}
try {
const pkg = JSON.parse(fs.readFileSync('./package.json','utf8')),
prodCmd = buildCommand(pkg.dependencies),
devCmd = buildCommand(pkg.devDependencies,'--save-dev');
if (prodCmd) {
console.log(prodCmd);
}
if (devCmd) {
console.log(devCmd);
}
} catch (ex) {
console.error(ex.message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment