假设我的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
索引.js
console.log(process.env.npm_lifecycle_event) // start-with-flag
包.json
{
"scripts": {
"start": "node index.js",
"start-with-flag": "node index.js -f"
}
}