【问题标题】:Determine npm or yarn script run from module确定从模块运行的 npm 或 yarn 脚本
【发布时间】:2018-04-21 13:44:16
【问题描述】:

假设我的package.json 文件中有以下脚本:

{
  "start": "node index.js",
  "start-with-flag": "node index.js -f"
}

Insideindex.js 我有一个console.log(process.argv),上面的脚本输出如下:

$ npm run start
[ '/usr/local/Cellar/node/8.4.0/bin/node',
  '/Users/.../test_app/index.js' ]

$ npm run start-with-flag
[ '/usr/local/Cellar/node/8.4.0/bin/node',
  '/Users/.../test_app/index.js',
  '-f' ]

是否可以检索我在index.js 内运行的脚本(startstart-with-flag)的值?

【问题讨论】:

    标签: node.js npm argv yarnpkg npm-scripts


    【解决方案1】:

    您可以使用npm_lifecycle_event 环境变量访问当前正在执行的脚本。

    命令

    $ npm run start-with-flag
    

    index.js

    console.log(process.env.npm_lifecycle_event)  // start-with-flag
    

    package.json

    {
      "scripts": {
        "start": "node index.js",
        "start-with-flag": "node index.js -f"
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-03-03
      • 2019-01-17
      • 2020-06-13
      • 2018-06-13
      • 1970-01-01
      • 2022-11-30
      • 1970-01-01
      • 2020-06-24
      • 1970-01-01
      相关资源
      最近更新 更多