根据参数dynamic安装节点包

我需要dynamic更新node.js package.json,例如,如果我运行npm start,它将会安装param A.

例如,如果我运行npm start A,它会启动

{ "name": "simplenodeapp", "main": "app.js", "scripts": { "start": "node app.js" <some param", }, "license": "ISC", "dependencies": { "express":"*" }, } 

如果我运行npm start B

 { "name": "simplenodeapp", "main": "app.js", "scripts": { "start": "node app.js" <some param>", }, "license": "ISC", "dependencies": { "HAproxy":"*" }, } 

可能吗 ? 我需要以编程方式执行…

更新:

我希望我终于明白被问到的问题。

NPM允许您定义configuration对象并传入dynamic参数。 这些可以单独使用或相当有效地组合使用。

在下面的评论中,OP询问了如何使用两个不同的文件名来运行节点。 这可以通过两种不同的方式来完成。

选项1:

 { "name": "test", "version": "1.0.0", "main": "index.js", "author": "Skyler Hair", "license": "MIT", "config": { "A": "index.js", "B": "app.js" }, "scripts": { "start:A": "node $npm_package_config_A", "start:B": "node $npm_package_config_B" } } 

npm run start:Anpm run start:B

选项2:

 { "name": "test", "version": "1.0.0", "main": "index.js", "author": "Skyler Hair", "license": "MIT", "scripts": { "start": "node" } } 

npm run start -- index.js或者npm run start -- app.js


原文答案:

您可以创build一个运行npm install的脚本,并接受依赖关系作为参数。 例如,

 { "name": "test", "version": "1.0.0", "main": "index.js", "license": "MIT", "author": "Skyler Hair", "scripts": { "install": "npm install" } } 

可以运行使用

npm run install -- express <other dependency> <another dep>