Search code examples
node.jsnpmgulp

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


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?


Solution

  • 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.