Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass command-line variable in package.json script, in NPM?

Tags:

node.js

npm

gulp

Pass command line args to npm scripts in package.json is almost what I'm looking for.

I use Gulp to do our NPM builds. I am able to do this, using the yargs plugin

$ gulp build --gitTag 1.0.0

and it produces dist/packageName-1.0.0.zip file. However, I need to be able to do this

$ npm run build --gitTag=1.0.0

I tried this,

  "scripts": {
    "build": "npm ci && gulp build --gitTag %npm_config_gitTag%"
  }

...and this

  "scripts": {
    "build": "export GIT_TAG=%npm_config_gitTag% && npm ci && gulp build"
  }

However, the %npm_config_gitTag% is not substituted with my passed-in gitTag argument, meaning the resulting artifact is packageName-%npm_config_gitTag%.zip

What should my build script look like in my package.json file?

like image 410
Chris F Avatar asked Feb 26 '26 10:02

Chris F


1 Answers

I found the magic recipe

I simply have this in my package.json

  "scripts": {
    "build": "npm ci && gulp build"
  }

and use the following command line

$ npm run build -- --gitTag 1.0.0

and get the wanted packageName-1.0.0.zip artifact.

like image 177
Chris F Avatar answered Mar 01 '26 11:03

Chris F



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!