Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 682
Dinis Cruz Avatar asked Jan 02 '15 02:01

Dinis Cruz


People also ask

What is scripts section in package json?

Scripts are stored in a project's package. json file, which means they're shared amongst everyone using the codebase. They help automate repetitive tasks, and mean having to learn fewer tools. Node npm scripts also ensure that everyone is using the same command with the same flags.

What is role on package json file in node JS?

The package. json file is the heart of any Node project. It records important metadata about a project which is required before publishing to NPM, and also defines functional attributes of a project that npm uses to install dependencies, run scripts, and identify the entry point to our package.

How does npm work with package json?

All npm packages contain a file, usually in the project root, called package. json - this file holds various metadata relevant to the project. This file is used to give information to npm that allows it to identify the project as well as handle the project's dependencies.


1 Answers

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
$ 
like image 177
Sam Mikes Avatar answered Oct 28 '22 00:10

Sam Mikes