【问题标题】:Get the value of another field in package.json file (npm)获取 package.json 文件 (npm) 中另一个字段的值
【发布时间】:2018-01-01 03:56:31
【问题描述】:

我有以下package.json 文件:

{
  "main": "./dist/index.js",
  "scripts": {
    "build": "tsc",
    "build:live": "tsc && node ./dist/index.js"
  },
  ...
}

如何获取main字段的值,然后将其放入命令node ./dist/index.js,类似这样(只是一个例子,我知道这个$是用于ENV变量的):

{
  "main": "./dist/index.js",
  "scripts": {
    "build": "tsc",
    "build:live": "tsc && node $main"
  },
  ...
}

提前致谢

【问题讨论】:

    标签: json node.js npm


    【解决方案1】:

    所有 package.json 字段都可以作为变量使用,方法是在它们前面加上 $npm_package_。在您的示例中,您可以使用:

    {
      "main": "./dist/index.js",
      "scripts": {
        "build": "tsc",
        "build:live": "tsc && node $npm_package_main"
      },
      ...
    }
    

    【讨论】:

      猜你喜欢
      • 2015-11-18
      • 1970-01-01
      • 2017-01-01
      • 2016-12-01
      • 2016-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多