假设我的 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
中运行的脚本(start
或 start-with-flag
)的值>?
最佳答案
您可以使用 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"
}
}
关于node.js - 确定从模块运行的npm或yarn脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47187029/