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

I am trying to use npm as a tool to run / build tasks after reading this article:

How to use npm as a build tool

and while I get some success, I am stuck on one. When running a global command line tool such as JSLINT, JSHINT or ESLINT, npm will always display exit code 1 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 information. Is this normal and / or is there a way to disable it for certain commands?

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

"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'

+6
source share
4 answers

I use this:

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

Hello!

+3
source

Since there are validation errors, eslint exists with exit code 1, which makes npm believe that there was an error during its execution.

If you are using linux, you can use this trick to always return exit code 0:

 "scripts": { "start": "node ./src/server/index.js", "test": "", "lint": "eslint index.js; true" } 
+2
source

NPM scripts are designed specifically for this, so exit code from 1 or 2 (nothing but 0 ) will prevent the launch of post-jobs, in the same way that it will work on your operating system.

Using the --silent flag is an option, but it can become a problem where there are other problems with the script, and you will hit your head against the wall when your builds fail without any attempt / error test.

The best thing you can do here ... is to configure your process so that it does not output an exit code with an error in situations where you do not want it. In this case ... you have some errors appearing legally based on your eslint configuration. This will cause the exit code to fail and (rightfully) prevent the next task from starting. This is really very useful when you use npm scripts because you can prevent the testing / build steps from being performed if you know there are errors.

So, in this case, you want to add the .eslintrc file to your project and specify some rules that will concern enumeration errors, as well as npm errors.

I sent a quick .eslintrc sample below. When you run eslint on the command line, it will automatically detect any .eslintrc or .eslintignore and save their configurations.

In this example below, your liter erros will be cleared, but keep in mind that it changes the exit code that will be thrown when eslint picks up this β€œtrigger”. When I change the rule to 0 , it means that it will not warn you when it recognizes this pattern.

You can learn more about using and configuring rule codes or ... check out the seed project I created, which uses npm as a build tool and enables the use of eslint: 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 } } 
+2
source

I found a workaround for this problem. In the script block, I use a test script to invoke the npm command for lint with the -silent flag:

 "scripts": { "start": "node ./src/server/index.js", "test": "npm run-script --silent lint", "lint": "eslint index.js"} 

Then at the command prompt I can type:

 npm run test 

Now it works without displaying an error log.

+1
source

Source: https://habr.com/ru/post/981578/


All Articles