Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass variable to npm command string (not the script run by npm command) [duplicate]

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!

like image 972
user1935987 Avatar asked Aug 30 '25 17:08

user1935987


1 Answers

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 =

like image 72
Boris Kukec Avatar answered Sep 02 '25 09:09

Boris Kukec