Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using npm as a task runner/build tool - having problems with some cli modules

I'm trying to use npm as a task runner/build tool after reading this article:

How to use npm as a build tool

and while I'm having some success, I'm stuck on one thing. When running a command-line global tool like JSLINT, JSHINT, or ESLINT, npm will always show the Exit 1 code in the console window:

enter image description here

As you can see, the command works fine, but npm sees it as an error and displays the error log info. Is this normal and/or is there a way to turn it off for specific commands?

Additional info: this is script block in my package.json config:

"scripts": { "start": "node ./src/server/index.js", "test": "", "lint": "eslint index.js" }

then from npm cli I type:

npm run lint

This will execute the script found in the package.json file with the label: 'lint'

like image 339
u84six Avatar asked Jan 26 '15 23:01

u84six


3 Answers

I use this:

"scripts": {
   "start": "node ./src/server/index.js",
   "lint": "eslint index.js || exit 0"
}

Regards!

like image 173
jacanotrigueros Avatar answered Sep 20 '22 01:09

jacanotrigueros


Since there are validation errors, eslint exists with an Exit code 1, which makes npm believe that there was an error during it's execution.

If you're using linux, you can use this trick to always return an Exit code 0 :

"scripts": { "start": "node ./src/server/index.js", "test": "", "lint": "eslint index.js; true" }
like image 43
ploutch Avatar answered Sep 22 '22 01:09

ploutch


NPM scripts are designed to work this way on purpose, so that an exit code of 1 or 2, (anything other than 0), will prevent post tasks from running, in the same way it would operate on your operating system.

Using the --silent flag is an option, but can become an issue where there are other problems with the script, and you will wind up banging your head against a wall when your builds start failing without any lint/test errors.

The best thing to do here... is to configure your process so that it doesn't output an error exit code in situations where you don't want it to. In this case... you have some errors popping up legitimately based on your eslint config. This will cause an error exit code, and (rightfully so) prevent the next task from running. This is actually very useful when you're using npm scripts, because you can prevent testing/build steps to be run unecessarily when you know there are errors.

So, in this case, you want to add an .eslintrc file to your project, and specify some rules that will both take care of the linting errors, as well as the npm errors.

I've posted a quick sample .eslintrc file below. When running eslint on the command line, it will automatically detect any .eslintrc or .eslintignore files, and abide by their configurations.

That sample below will clean up your linting erros, but keep in mind, it changes the exit code that would be thrown when eslint picks up that "trigger". When I change a rule to 0, it means that it won't alert you when it recognizes that pattern.

You can read more about utilizing and configuring rule codes, or... check out a seed project I have created that uses npm as a build tool, and includes eslint usage: react-flux-npm-automation

// /path/to/project/.eslintrc
{
    "parser": "babel-eslint",
    "env":{
        "browser":true,
        "node":true,
        "es6":true
    },
    "rules": {
        "strict":0,
        "quotes":0,
        "no-unused-vars":0
    }
}
like image 20
Brian Vanderbusch Avatar answered Sep 21 '22 01:09

Brian Vanderbusch