1

I need to update the node.js package.json dynamically for example if I run npm start with param A it will install

for example if I run npm start A it will start

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

}

And if I run npm start B

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

}

is it possible ? I need to do it programmatically...

  • Possible duplicate of [Sending command line arguments to npm script](https://stackoverflow.com/questions/11580961/sending-command-line-arguments-to-npm-script) – Sky Jun 11 '17 at 15:33
  • @Sky - well, so how should I update the dependencies ? –  Jun 11 '17 at 15:42

1 Answers1

3

UPDATE:

I hope I finally understand the question being asked.

NPM allows you to define config objects and pass in dynamic arguments. These can be used separately or combined quite powerfully.

In the comments below, the OP asked how to run node with two different file names. This can be accomplished in two different ways.

Option 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:A OR npm run start:B

Option 2:

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

npm run start -- index.js OR npm run start -- app.js


ORIGINAL ANSWER:

You can create a script that runs npm install and accepts the dependencies as arguments. For example,

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

can be run using

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

Sky
  • 372
  • 2
  • 7
  • Thanks this is half of the answer but i'll give you 1+ since it help, what I miss here is how can I change the start script e.g. if I use "npm run install -- express" the start should be "start": "node app.js" and if I run "npm run install -- lodash the start should be "start": "node index.js" , the difference is for example the start file here ... –  Jun 11 '17 at 16:02
  • Thanks we are close :) thanks , the only thing that I miss is how I should do it programmatically ,maybe via gurnt this is the reason that I tagged it but not sure ? –  Jun 11 '17 at 18:00
  • Depends on your use case. Why do you need to launch it programmatically? Will it always be run locally? Will you be deploying to a server? etc. – Sky Jun 11 '17 at 18:24
  • 1. tldr , Im generating a program that need to update the package json according to some parameters ...2. what do you mean run locallay always ? 3. yes it should be deployed –  Jun 11 '17 at 18:48