确定从模块运行的 npm 或 yarn 脚本



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

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

index.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)的值?

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

命令

$ npm run start-with-flag

索引.js

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

包.json

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

最新更新