Passing argument to the middle of an npm script

Per Passing args into run-scripts #5518 it would appear that is it not possible to pass arguments to the middle of the script.

We are not going to support passing args into the middle of the script, sorry. If you really need this, write your test command using literally any of the command line parsers that anyone uses. (Minimist, dashdash, nopt, and commander all support this just fine.)

However, an alternative to this using the npm configuration block has been documented here. My implementation then looks like this:

"name": "foo"
"config": { "destination" : "deploy" },
"scripts": { "deploy": "robocopy dist %npm_package_config_destination% /E /NP" }

I can then override this on the command line and on my build server with:

npm run deploy --foo:destination=C:\path\to\deploy\dir

You can use an environment variable to set the destination path.

PATH=/path/to/file npm run deploy -- $PATH

or

export PATH=/path/to/file

npm run deploy -- $PATH

Tags:

Npm