Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am getting different results running a script with npm than calling the command directly - Node

Tags:

node.js

I am setting up my environment to lint ES6. I installed eslint and can see it in my node_modules. In my node_modules/bin directory, I have the command eslint.

I can run the command and point it at the directory and I get no errors:

./node_modules/.bin/eslint src/main/webapp/js/lcrf

I can see all of the linting errors that I need to fix.

I have also added the command into my package.json:

  "scripts": {
    "lint": "eslint src/main/webapp/js/lcrf"
  },

Now I try to run the command with npm run lint. It lints my files and I get the same number of linting errors, but then node errors out. Here is my stacktrace:

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! [email protected] lint: `eslint src/main/webapp/js/lcrf`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] 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.

What would cause this to happen? What is the difference in the 2 ways that I am running the command?

like image 721
jhamm Avatar asked May 12 '15 21:05

jhamm


1 Answers

npm is freaking out over the non-zero return code from eslint. If you want lint errors to be an indication of "whoa, that's not supposed to happen, red alert, something is really wrong if this got published" then that's what you've got.

If you don't use the exit code (say, to stop subsequent build steps) and you just want the output to be just the eslint output and no subsequent npm freak out, use this in your package.json:

"lint": "eslint src/main/webapp/js/lcrf || exit 0"

Note this in "Best Practices" section of the npm scripts docs:

Don't exit with a non-zero error code unless you really mean it. Except for uninstall scripts, this will cause the npm action to fail, and potentially be rolled back. If the failure is minor or only will prevent some optional features, then it's better to just print a warning and exit successfully.

like image 145
Trott Avatar answered Sep 21 '22 13:09

Trott