Last active
February 21, 2026 23:16
-
-
Save magnetikonline/fb9246d26433986b475725a318ec38c4 to your computer and use it in GitHub Desktop.
Generate `npm install` commands from existing package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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