In node package.json, invoke script from another script with extra parameter, in this case add mocha watcher

in node's package.json I would like to reuse a command that I already have in a 'script'.

Here is the practical example

instead of (note the extra -w on the watch script):

"scripts": {
             "test" : "./node_modules/mocha/bin/mocha --compilers coffee:coffee-script/register --recursive -R list",
             "watch": "./node_modules/mocha/bin/mocha --compilers coffee:coffee-script/register --recursive -R list -w",
           }

I would like to have something like

"scripts": {
             "test" : "./node_modules/mocha/bin/mocha --compilers coffee:coffee-script/register --recursive -R list",
             "watch": "npm run script test" + "-w",
           }

which doesn't work (can't do string concats in json), but you should get what I would like

I know that npm scripts support: - & (parallel execution) - && (sequencial execution)

so maybe there is another option?


This can be done in [email protected]. You don't specify your OS and the version of npm that you are using, but unless you have done something to update it, you are probably running [email protected] which does not support the syntax below.

On Linux or OSX you can update npm with sudo npm install -g npm@latest. See https://github.com/npm/npm/wiki/Troubleshooting#try-the-latest-stable-version-of-npm for a guide to updating npm on all platforms.

You should be able to do this by passing an additional argument to your script:

"scripts": {
  "test": "mocha --compilers coffee:coffee-script/register --recursive -R list",
  "watch": "npm run test -- -w"
}

I verified this using the following, simplified package.json:

{
  "scripts": { "a": "ls", "b": "npm run a -- -l" }
}

Output:

$ npm run a

> @ a /Users/smikes/src/github/foo
> ls

package.json
$ npm run b

> @ b /Users/smikes/src/github/foo
> npm run a -- -l


> @ a /Users/smikes/src/github/foo
> ls -l

total 8
-rw-r--r--  1 smikes  staff  55  4 Jan 05:34 package.json
$