【问题标题】:How to pass command-line variable in package.json script, in NPM?如何在 NPM 中的 package.json 脚本中传递命令行变量?
【发布时间】:2022-01-14 02:42:58
【问题描述】:

Pass command line args to npm scripts in package.json 几乎就是我要找的。​​p>

我使用 Gulp 进行 NPM 构建。我能够做到这一点,使用yargs plugin

$ gulp build --gitTag 1.0.0

它会生成dist/packageName-1.0.0.zip 文件。但是,我需要能够做到这一点

$ npm run build --gitTag=1.0.0

我试过了,

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

...还有这个

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

但是,%npm_config_gitTag% 不会被我传入的 gitTag 参数替换,这意味着生成的工件是 packageName-%npm_config_gitTag%.zip

我的build 脚本在我的package.json 文件中应该是什么样的?

【问题讨论】:

    标签: node.js npm gulp


    【解决方案1】:

    我找到了秘方

    我只是在我的package.json 中有这个

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

    并使用以下命令行

    $ npm run build -- --gitTag 1.0.0
    

    并获得想要的packageName-1.0.0.zip 工件。

    【讨论】:

      猜你喜欢
      • 2020-05-09
      • 2018-01-11
      • 1970-01-01
      • 2022-08-17
      • 2016-12-05
      • 2021-11-12
      • 2012-10-31
      相关资源
      最近更新 更多