【问题标题】:How can I use variables with npm 7.x.x in package.json?如何在 package.json 中使用带有 npm 7.x.x 的变量?
【发布时间】:2021-05-13 08:15:17
【问题描述】:

我想使用 npm 脚本访问 directories 中的密钥。

  "directories": {
    "client": "client",
    "server": "server"
  },
  "scripts": { 
    "test:client": "npm run local-tests --prefix ./$npm_package_directories_client", 
  }

但是在将 npm 从 6.x.x 更新到 7.5.x 之后,我不能再这样做了。

我如何使用新的 npm 做到这一点?

谢谢

【问题讨论】:

标签: javascript node.js typescript npm


【解决方案1】:

我可以通过将我的变量移动到"config": {…}下来解决它。

像这样:

  "config": {
    "directories": {
      "client": "client",
      "server": "server"
    }
  },
  "scripts": { 
    "test:client": "npm run local-tests --prefix ./$npm_package_config_directories_client", 
  }

https://docs.npmjs.com/cli/v7/configuring-npm/package-json#config

【讨论】:

    【解决方案2】:

    同样的问题!使用您的解决方案解决!谢了哥们!我建议一种向我们的 json 文件添加注释的简单方法。

      "config": {
         "@comments devPath": "Path to build the develeping code.",
         "devPath": "../castorweb/src/dist/webapps/AQSAPP_TEMPLATE/tmp",
         "@comments prodPath": "Path to build the production code: When generating app versions",
         "prodPath": "../castorweb/src/dist/webapps/AQSAPP_TEMPLATE/dist"
      },
    

    【讨论】:

      【解决方案3】:

      这个问题的解决方法如下:

      "workspaces": [
         "workspace-a"
      ]
      "scripts": { 
         "test:client": "npm run local-tests -w", 
      }
      

      查看 npm 工作区docs

      【讨论】:

        猜你喜欢
        • 2021-11-12
        • 2016-12-05
        • 1970-01-01
        • 2019-05-27
        • 2017-09-28
        • 2016-01-03
        • 1970-01-01
        • 2018-10-17
        • 2018-12-03
        相关资源
        最近更新 更多