Eslint: How to disable "unexpected console statement" in Node.js?

293,076

Solution 1

Create a .eslintrc.js in the directory of your file, and put the following contents in it:

module.exports = {
    rules: {
        'no-console': 'off',
    },
};

Solution 2

You should update eslint config file to fix this permanently. Else you can temporarily enable or disable eslint check for console like below

/* eslint-disable no-console */
console.log(someThing);
/* eslint-enable no-console */

Solution 3

For vue-cli 3 open package.json and under section eslintConfig put no-console under rules and restart dev server (npm run serve or yarn serve)

...
"eslintConfig": {
    ...
    "rules": {
      "no-console": "off"
    },
    ...

Solution 4

The following works with ESLint in VSCode if you want to disable the rule for just one line.

To disable the next line:

// eslint-disable-next-line no-console
console.log('hello world');

To disable the current line:

console.log('hello world'); // eslint-disable-line no-console

Solution 5

A nicer option is to make the display of console.log and debugger statements conditional based on the node environment.

  rules: {
    // allow console and debugger in development
    'no-console': process.env.NODE_ENV === 'production' ? 2 : 0,
    'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0,
  },
Share:
293,076
Jean Y.C. Yang
Author by

Jean Y.C. Yang

A Taiwanese / Formosan programmer^_^ [PAST] PHP MySQL JAVA Android jQuery Javascript AngularJS ReactJS React Native [NOW] ReactJS Redux Node.js Express.js Golang Docker Kubernetes Helm ElasticSearch Redis MongoDB MySQL Bash

Updated on January 18, 2022

Comments

  • Jean Y.C. Yang
    Jean Y.C. Yang over 2 years

    I'm using eslint with Sublime Text 3 and I am writing gulpfile.js.

    /*eslint-env node*/
    var gulp = require('gulp');
    
    gulp.task('default', function(){
        console.log('default task');
    });
    

    But eslint keeps showing error : "Error: Unexpected console statement. (no-console)" eslint error

    I found official document here, but I still don't know how to disable it.

    /*eslint-env node*/
    var gulp = require('gulp');
    
    /*eslint no-console: 2*/
    gulp.task('default', function(){
        console.log('default task');
    });
    

    doesn't work, either.

    My Sublime Text 3 plugins: SublimeLinter and SublimeLinter-contrib-eslint.

    Here's my .eslintrc.js file:

    module.exports = {
        "rules": {
            "no-console":0,
            "indent": [
                2,
                "tab"
            ],
            "quotes": [
                2,
                "single"
            ],
            "linebreak-style": [
                2,
                "unix"
            ],
            "semi": [
                2,
                "always"
            ]
        },
        "env": {
            "browser": true,
            "node": true
        },
        "extends": "eslint:recommended"
    };
    
  • markasoftware
    markasoftware over 8 years
    Well, according to the official github page for the eslint plugin (github.com/roadhump/…), putting a .eslintrc file into your project folder should do the trick...to continue debugging it, I'd recommend trying to run eslint from the command line. Simply install node.js if you don't have it installed, then run npm install eslint from a console/command prompt, then navigate to your project folder in a console/command prompt, and run eslint .
  • Jean Y.C. Yang
    Jean Y.C. Yang over 8 years
    It remains the same problem. i.imgur.com/nIvrUih.png This is also useless.
  • markasoftware
    markasoftware over 8 years
    hmm, this is very strange...could you create the .eslintrc file that I mentioned in the answer in the same folder as your file, then run: eslint gulpfile.js -c .eslintrc and see if it works then?
  • Jean Y.C. Yang
    Jean Y.C. Yang over 8 years
    My file is .eslintrc.js not .eslintrc, is it correct?
  • Jean Y.C. Yang
    Jean Y.C. Yang over 8 years
    How to config my .eslintrc, please tell me?
  • markasoftware
    markasoftware over 8 years
    @JeanYang yeah, that's an issue. Name it just .eslintrc, and then it should work in sublime just fine.
  • Jean Y.C. Yang
    Jean Y.C. Yang over 8 years
    If i rename it to .eslintrc, the console throws an error. The console mistook it as a YAML file. i.imgur.com/ffavJJT.png
  • markasoftware
    markasoftware over 8 years
  • AlexWien
    AlexWien over 7 years
    it works (for me) when the file is called .eslintrc.json
  • Chunky Chunk
    Chunky Chunk over 7 years
    Alternatively, you can write "rules": {"no-console": "off"} to be less cryptic. "warn" and "error" being the other 2 options.
  • Colin D Bennett
    Colin D Bennett almost 7 years
    The ESLint configuration file used to be simply .eslintrc but now that is deprecated and should be named according to the format used, e.g. .eslintrc.js, .eslintrc.yaml, etc.
  • markasoftware
    markasoftware almost 7 years
    @ColinDBennett i said .js in my answer, not sure what the problem is.
  • user234461
    user234461 over 6 years
    Even better, just run this handy script on all your files: find . -name '*.js' -exec gawk -i inplace 'NR==1{print "/* eslint-disable */"} {print}' {} \;
  • Petros Kyriakou
    Petros Kyriakou over 6 years
    this syntax won't work for a .js file, thats .json
  • markasoftware
    markasoftware over 6 years
    it will now @PetrosKyriakou!
  • Lonnie Best
    Lonnie Best almost 6 years
    A .js config is easier to use than a .json config for me. I can have trailing commas in a .js file, but .json config won't allow it.
  • Jonathan Brizio
    Jonathan Brizio over 5 years
    Not is necessary to add both lines. With only put previous of your console.log the following exception is enough: eslint-disable-next-line no-console.
  • muenalan
    muenalan over 5 years
    Thanks, as @markasoftware solution does not work if you come here for a cue-cli project.
  • swiesend
    swiesend over 5 years
    this is helpful if one generated a vue project trough vue-cli or vue ui and it contains a vue.config.js and package.json. Edit the package.json.
  • Michael Cole
    Michael Cole about 5 years
    package.json isn't the only way to do this. Separate config files are also a standar.
  • German Latorre
    German Latorre almost 5 years
    Does not work in vue-cli 3: see answer stackoverflow.com/a/53333105/150370
  • Ank
    Ank over 4 years
    Thanks! You saved my day.
  • Denis
    Denis over 4 years
  • Denis
    Denis over 4 years
  • MaxRocket
    MaxRocket over 4 years
    Which package.json file?
  • GiorgosK
    GiorgosK over 4 years
    the one in the root folder @MaxRocket
  • Sanyam Jain
    Sanyam Jain almost 4 years
    console msgs still print in production environment
  • Scott Baker
    Scott Baker almost 4 years
    Needs to be JSON, not a JavaScript Object
  • Admin
    Admin almost 4 years
    how would this work if .eslintrc is in JSON not regular js file?
  • Nicke Manarin
    Nicke Manarin almost 4 years
    Interesting, that what happened to me. Why did we need to run npm install again? Or perhaps I just needed to restart with npm run serve.
  • Gael
    Gael over 2 years
    Thanks @JonathanBrizio that's exactly what I wanted. A quick and dirty solution to debug something. When I'm done, I will remove the console.log line. I don't want to permanently modify the eslint rules.
  • bfontaine
    bfontaine over 2 years
    .eslintrc.js is a JS file, so it does not need to be JSON.