28

I'd like to pass an argument into the first of two commands in my npm script:

"scripts": {
  "myscript": "a && b"
}

When I run npm run myscript -- --somearg=somevalue, the argument is passed to command b but not command a. Is there some way to instead ensure that the first command receives the argument?

Keir
  • 440
  • 6
  • 15
  • 3
    I think `npm ` appends the additional parameters to the configured value ( `a & b + $@`) and not for every command specified. As a dirty workaround you could specify a shell script `sh – Royal Pinto Feb 16 '16 at 11:11
  • See answer under http://stackoverflow.com/questions/35221098/passing-arguments-to-npm-script-in-package-json – aleung Oct 22 '16 at 04:29
  • Or [this answer](https://stackoverflow.com/questions/51388921/pass-command-line-args-to-npm-scripts-in-package-json/51401577#answer-51401577) for a solution which runs cross-platform (i.e. Bash, Windows Command Prompt / cmd.exe, and PowerShell etc..). – RobC Oct 17 '18 at 09:23
  • Perhaps what you need is the `pre` and `post` hooks for scripts? The docs describe that if you add a script prefixed with `pre` or `post`, like `postmyscript`, it will run after your script. However, your script is the one that gets the arguments. – 4thex Oct 22 '18 at 23:17

5 Answers5

1
"scripts": {
  "myscript": "a",
  "postmyscript": "b"
}
4thex
  • 1,094
  • 1
  • 9
  • 21
1

I found the following solution after searching a lot from this discussion:

https://github.com/npm/npm/issues/9627#issuecomment-152607809

and

https://github.com/npm/npm/issues/9627#issuecomment-178576802

Solution 1. So in your package json add a config object like so

{
  "name": "packageName",
  "config" : { "variable1" : "value1" }
}

then you can access the variable like so on windows

    {
      "name": "packageName",
      "config" : { "variable1" : "value1" },
      "scripts": {
              "myscript": "a -c %npm_package_config_variable1%  && b -c %npm_package_config_variable1%"
      }
    }

in mac/linux I am assuming (not a 100%)

{
  "name": "packageName",
  "config" : { "variable1" : "value1" },
  "scripts": {
          "myscript": "a -c $npm_package_config_variable1  && b -c $npm_package_config_variable1"
  }
}

then you can override the variable by calling the script like so:

npm run myscript --packageName:variable1=value2

Solution 2.

no need for the config entry in the package.json

{
  "name": "packageName",
  "scripts": {
          "myscript": "a -c %npm_config_variable1%  && b -c %npm_config_variable1%"
  }
}

and call it like so

npm run myscript --variable1=value2

reason to pick one over the other:

https://github.com/npm/npm/issues/9627#issuecomment-178813965

abann sunny
  • 928
  • 12
  • 15
1

Here a simple solution when working with Yarn v2 "berry":

"scripts": {
  "myscript": "a $@ && b $@"
}

Since yarn berry hosts it's own little cross-platform bash-like environment for executing the scripts in a package, this may work cross platform as well.

mxro
  • 6,588
  • 4
  • 35
  • 38
0

As alternative way, this is work with npm less additional setting.

But required bash(or zsh and so on).

"scripts": {
  "myscript": "bash -c 'a $@ && b $@' bash"
}

(last word 'bash' is needed(but no used), because of first arg set to $0, not included $@)

ynishi
  • 316
  • 2
  • 2
-2
npm run myscript -- --foo=1 --bar=2

See https://docs.npmjs.com/cli/run-script

geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
robdrake
  • 1
  • 2