The scripts portion of my package.json currently looks like this:
"scripts": {
"start": "node ./script.js server"
}
…which means I can run npm start to start the server. So far so good.
However, I would like to be able to run something like npm start 8080 and have the argument(s) passed to script.js (e.g. npm start 8080 => node ./script.js server 8080). Is this possible?
npm 2 and newer
It’s possible to pass args to
npm runsince npm 2 (2014). The syntax is as follows:npm run <command> [-- <args>]Note the
--separator, used to separate the params passed tonpmcommand itself, and the params passed to your script. (This is a common convention used by various command line tools).With the example
package.json:here’s how to pass the params to those scripts:
Note: If your param does not start with
-or--, then having an explicit--separator is not needed; but it’s better to do it anyway for clarity.Note below the difference in behavior (
test.jshasconsole.log(process.argv)): the params which start with-or--are passed tonpmand not to the script, and are silently swallowed there.The difference is clearer when you use a param actually used by npm:
To get the parameter value, see this question. For reading named parameters, it’s probably best to use a parsing library like yargs or minimist; nodejs exposes
process.argvglobally, containing command line parameter values, but this is a low-level API (whitespace-separated array of strings, as provided by the operating system to the node executable).