84

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?

Dinis Cruz
  • 4,161
  • 2
  • 31
  • 49

1 Answers1

113

This can be done in npm@2.1.17. 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 npm@1.4.28 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
$ 
Jan Trienes
  • 2,501
  • 1
  • 16
  • 28
Sam Mikes
  • 10,438
  • 3
  • 42
  • 47
  • perfect that did it, thanks. Here is the commit with the fix https://github.com/o2platform/fluentnode/commit/a2508df1d295d17ddbea8c7ac6c204d86886409d – Dinis Cruz Jan 04 '15 at 16:29
  • I was missing the "npm run" when calling another script from a script. Thanks! – Matthew Rideout Jan 18 '18 at 18:14
  • 5
    TIL the `--` tells `npm run` not to look for options after the `--` (in this case `-w`). – Chris Hayes Feb 03 '20 at 02:58
  • 3
    Worth noting that in the shell (bash, zsh, etc), the `--` double dash is used by most (not all) commands to indicate 'end of options' `man bash` gives this info: `A -- signals the end of options and disables further option processing. Any arguments after the -- are treated as filenames and arguments. An argument of - is equivalent to --.` – Bernard Leech Apr 19 '20 at 10:24
  • 1
    What if we use yarn and yarn is not installed npm is installed or vice versa??? – Faisal Manzer May 14 '20 at 20:47
  • @FaisalManzer this u?? :P https://gist.github.com/coryhouse/b26f49bead69066844d9#gistcomment-2980170 – Sebastian Thomas Mar 24 '21 at 13:35