根据参数动态安装node包

Install node package dynamically according to parameters

例如,我需要动态更新 node.js package.json 如果我 运行 npm start with 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 允许您定义配置对象并传入动态参数。这些可以单独使用,也可以非常强大地组合使用。

在下面的评论中,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.jsnpm run start -- app.js


原始答案:

您可以创建一个 运行s 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>