我得到不同的结果运行与npm脚本比直接调用命令 – 节点
我正在设置我的环境来ES6
。 我安装eslint
并可以在我的node_modules
看到它。 在我的node_modules/bin
目录中,我有命令eslint
。
我可以运行命令并将其指向目录,并且不会出现错误:
./node_modules/.bin/eslint src/main/webapp/js/lcrf
我可以看到所有需要修复的错误。
我也将这个命令添加到我的package.json
:
"scripts": { "lint": "eslint src/main/webapp/js/lcrf" },
现在我尝试用npm run lint
运行命令。 它提供了我的文件,并得到相同数量的lint
错误,但是然后node
出错。 这是我的堆栈跟踪:
npm ERR! Darwin 14.3.0 npm ERR! argv "node" "/usr/local/bin/npm" "run" "lint" npm ERR! node v0.12.2 npm ERR! npm v2.7.4 npm ERR! code ELIFECYCLE npm ERR! lcrf@0.0.0 lint: `eslint src/main/webapp/js/lcrf` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the lcrf@0.0.0 lint script 'eslint src/main/webapp/js/lcrf'. npm ERR! This is most likely a problem with the lcrf package, npm ERR! not with npm itself. npm ERR! Tell the author that this fails on your system: npm ERR! eslint src/main/webapp/js/lcrf npm ERR! You can get their info via: npm ERR! npm owner ls lcrf npm ERR! There is likely additional logging output above.
什么会导致这种情况发生? 我在执行命令的两种方式有什么不同?
npm
正在通过eslint
的非零返回代码eslint
。 如果你想让lint错误成为“哇,这不应该发生,红色警报,如果这个发布是真的错误”的指示,那么这就是你所拥有的。
如果你不使用退出代码(比如说停止后续的构build步骤),而你只是希望输出只是eslint
输出,并且没有后续的npm
eslint
,那么在你的package.json
使用它:
"lint": "eslint src/main/webapp/js/lcrf || exit 0"
在npm脚本文档的“最佳实践”部分中注意:
除非你确实是这个意思,否则不要用非零的错误代码退出。 除了卸载脚本之外,这将导致npm操作失败,并可能被回滚。 如果失败是轻微的,或者只会阻止某些可选function,那么最好只打印一个警告并成功退出。