6

This is my npm package.json scripts:

"scripts": {
  "method1": "./somScript $1",
  "method2": "webpack-dev-server",
  "runBoth": "npm run method1 $1 & npm run method2"
},

I want to be able to run npm run runBoth someArgument that would get passed to runBoth, and then to method1.

However, my code above does not work. Please advice!

Kousha
  • 32,871
  • 51
  • 172
  • 296

1 Answers1

0

No, unfortunately it's not possible as NPM appends arguments.

You are executing: npm run method1 & npm run method2 "someArgument"

So, I guess your output is as follows:

[nodejs@mean test]$ npm run runBoth someArgument

> stackOverflow@0.0.1 runBoth /data/nodejs/node_projects/test
> npm run method1 $1 & npm run method2  "someArgument"


> stackOverflow@0.0.1 method1 /data/nodejs/node_projects/test
> ./somScript

[somScript] argument:

> stackOverflow@0.0.1 method2 /data/nodejs/node_projects/test
> ./script2.sh "someArgument"

now executing script2.sh using arg: someArgument
[nodejs@mean test]$

Also please note that arguments should be passed using "--" as follows:

[nodejs@mean test]$ npm run runBoth -- someArgument

> stackOverflow@0.0.1 runBoth /data/nodejs/node_projects/test
> npm run method1 $1 & npm run method2  "someArgument"


> stackOverflow@0.0.1 method1 /data/nodejs/node_projects/test
> ./somScript

[somScript] argument:

> stackOverflow@0.0.1 method2 /data/nodejs/node_projects/test
> ./script2.sh "someArgument"

now executing script2.sh using arg: someArgument
[nodejs@mean test]$

But in your case, the expected output will be the same.

My package.json:

  "scripts" : {
    "method1": "./somScript",
    "method2": "./script2.sh",
    "runBoth": "npm run method1 $1 & npm run method2 "
  }

You should try another implementation (as exporting/using environment variables, or others)

Here's a possible workaround:

package.json:

  "scripts" : {
    "method1": "./somScript",
    "method2": "./script2.sh",
    "runBoth": "npm run method1 $1 & npm run method2 ",
    "runBoth2": "./alternative.sh"
  }

Where runBoth2 executes "alternative.sh" as:

[nodejs@mean test]$ cat alternative.sh
npm run method1 $1 & npm run method2

And the expected output:

[nodejs@mean test]$ npm run runBoth2 -- someArgument

> stackOverflow@0.0.1 runBoth2 /data/nodejs/node_projects/test
> ./alternative.sh "someArgument"


> stackOverflow@0.0.1 method1 /data/nodejs/node_projects/test
> ./somScript "someArgument"

[somScript] argument: someArgument

> stackOverflow@0.0.1 method2 /data/nodejs/node_projects/test
> ./script2.sh

now executing script2.sh using arg:
[nodejs@mean test]$

Regards

Daniel Vukasovich
  • 1,692
  • 1
  • 18
  • 26