4

Is it possible to pass an argument to the npm command string? The info might be somewhere in docs, but brief digging didn't get me any results, moreover, it is probably beneficial to the large audience.

For example, i have a command build:docker in package.json:

   "scripts": {
      "build:docker": "tsc -b && sh dist.sh && docker build -t repo_name/image .",
   },

I want to pass the tag variable to the command string, ex:

"build:docker": "tsc -b && sh dist.sh && docker build -t repo_name/image:$tag ."

and run it as npm run build:docker --tag '1.0'.

Is something like this possible? Thanks!

11
  • 1
    Use a shell function. For example redefine your npm script as: "build:docker": "func() { tsc -b && sh dist.sh && docker build -t \"repo_name/image:${1}\" .; }; func" - then run npm run build:docker -- "1.0"
    – RobC
    Commented Feb 12, 2021 at 17:08
  • Given that you want to pass "1.0" to the npm script, should the last part of your npm script example form: ... && docker build -t repo_name/image:1.0 ." ? Presumable the $tag in your example is a placeholder for where 1.0 should go, or did I misunderstand?
    – RobC
    Commented Feb 12, 2021 at 17:27
  • yeah, thats right Commented Feb 12, 2021 at 17:37
  • So, have you tried what I suggested in my first comment?
    – RobC
    Commented Feb 12, 2021 at 17:37
  • yes, testing it now. seems like the param was passed correctly, however for some reasons the whole image is re-assembling. but the resulting repo_name/image:1.0 was correct by logs - weird why is it re-building. Commented Feb 12, 2021 at 17:38

1 Answer 1

2

Yes, you can: For example (for simplicity), for the line:

"build": "tsc --project "

You can start it like so:

npm run build ./src

This will start:

> tsc --project  "./src"

And another solution (I didn't see the point without glasses :) ), more suitable for you is to prefix the name of the parameter with $npm_config_, so your line should look like this:

"build:docker": "tsc -b && sh dist.sh && docker build -t repo_name/image:$npm_config_mytag ."

And then run it as:

npm run build:docker --mytag=1.0

Watch out for =

4
  • 1
    this will pass arguments as string and add them strictrly at the last position. in the sample command tag argument is not at the last position. Commented Feb 12, 2021 at 17:13
  • I have added the 2nd possibility, which is more suitable. Commented Feb 12, 2021 at 21:25
  • 2
    npm creates its own environment variable named npm_config_tag for internal purposes. It's value is typically set to latest. So, just be aware that you are overriding this internal env var with a new custom value (i.e. 1.0) - so this has potential to produce unwanted results. If you cd to a project directory that contains a package.json, then run npm run env it will print many of the npm_ prefixed variables that npm adds to the environment. You should find that npm_config_tag=latest already exists.
    – RobC
    Commented Feb 13, 2021 at 20:24
  • Thanks, @RobC you are right. Commented Feb 14, 2021 at 14:07

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.