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

Javascriptnode.jsSublimetext3Sublime Text-PluginEslint

Javascript Problem Overview


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"
};

Javascript Solutions


Solution 1 - Javascript

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 - Javascript

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 - Javascript

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 - Javascript

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 - Javascript

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,
  },

Solution 6 - Javascript

If you install eslint under your local project, you should have a directory /node_modules/eslint/conf/ and under that directory a file eslint.json. You could edit the file and modify "no-console" entry with the value "off" (although 0 value is supported too):

"rules": {
    "no-alert": "off",
    "no-array-constructor": "off",
    "no-bitwise": "off",
    "no-caller": "off",
    "no-case-declarations": "error",
    "no-catch-shadow": "off",
    "no-class-assign": "error",
    "no-cond-assign": "error",
    "no-confusing-arrow": "off",
    "no-console": "off",
    ....

I hope this "configuration" could help you.

Solution 7 - Javascript

If you just want to disable the rule once, you want to look at Exception's answer.

You can improve this by only disabling the rule for one line only:

... on the current line:

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

... or on the next line:

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

Solution 8 - Javascript

Alternatively instead of turning 'no-console' off, you can allow. In the .eslintrc.js file put

  rules: {
    "no-console": [
     "warn",
     { "allow": ["clear", "info", "error", "dir", "trace", "log"] }
    ]
  }

This will allow you to do console.log and console.clear etc without throwing errors.

Solution 9 - Javascript

I'm using Ember.js which generates a file named .eslintrc.js. Adding "no-console": 0 to the rules object did the job for me. The updated file looks like this:

module.exports = {
  root: true,
  parserOptions: {
    ecmaVersion: 6,
    sourceType: 'module'
  },
  extends: 'eslint:recommended',
  env: {
    browser: true
  },
  rules: {
    "no-console": 0
  }
};

Solution 10 - Javascript

in my vue project i fixed this problem like this :

vim package.json
...
"rules": {
	"no-console": "off"
},
...

ps : package.json is a configfile in the vue project dir, finally the content shown like this:

{
  "name": "metadata-front",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "axios": "^0.18.0",
    "vue": "^2.5.17",
    "vue-router": "^3.0.2"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "^3.0.4",
    "@vue/cli-plugin-eslint": "^3.0.4",
    "@vue/cli-service": "^3.0.4",
    "babel-eslint": "^10.0.1",
    "eslint": "^5.8.0",
    "eslint-plugin-vue": "^5.0.0-0",
    "vue-template-compiler": "^2.5.17"
  },
  "eslintConfig": {
    "root": true,
    "env": {
      "node": true
    },
    "extends": [
      "plugin:vue/essential",
      "eslint:recommended"
    ],
    "rules": {
    	"no-console": "off"
    },
    "parserOptions": {
      "parser": "babel-eslint"
    }
  },
  "postcss": {
    "plugins": {
      "autoprefixer": {}
    }
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not ie <= 8"
  ]
}

Solution 11 - Javascript

If you're still having trouble even after configuring your package.json according to the documentation (if you've opted to use package.json to track rather than separate config files):

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

And it still isn't working for you, don't forget you need to go back to the command line and do npm install again. :)

Solution 12 - Javascript

2018 October,

just do:

// tslint:disable-next-line:no-console

the anothers answer with

// eslint-disable-next-line no-console

does not work !

Solution 13 - Javascript

In package.json you will find an eslintConfig line. Your 'rules' line can go in there like this:

  "eslintConfig": {
   ...
    "extends": [
      "eslint:recommended"
    ],
    "rules": {
      "no-console": "off"
    },
   ...
  },

Solution 14 - Javascript

You should add one rule and add your env:

{
  "rules": {
    "no-console": "off"
  },
  "env": {
    "browser": true
  }
}

you can add other envs.

Solution 15 - Javascript

in "rules", "no-console": [false, "log", "error"]

Solution 16 - Javascript

My 2 cents contribution:

Besides removing the console warning (as shown above), it's best to remove yours logs from PROD environments (for security reasons). The best way I found to do so, is by adding this to nuxt.config.js

  build: {
   terser: {
      terserOptions: {
        compress: {
          //this removes console.log from production environment
          drop_console: true
        }
      }
    }
  }

How it works: Nuxt already uses terser as minifier. This config will force terser to ignore/remove all console logs commands during compression.

Solution 17 - Javascript

make sure that the name of the folder that the flutter project is in. Doesn't have spaces. that was my error

Solution 18 - Javascript

Use Window Object

window.console.log("..")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJean Y.C. YangView Question on Stackoverflow
Solution 1 - JavascriptmarkasoftwareView Answer on Stackoverflow
Solution 2 - JavascriptExceptionView Answer on Stackoverflow
Solution 3 - JavascriptGiorgosKView Answer on Stackoverflow
Solution 4 - JavascriptbiniciView Answer on Stackoverflow
Solution 5 - JavascriptFrank SpinView Answer on Stackoverflow
Solution 6 - JavascriptJose M BelView Answer on Stackoverflow
Solution 7 - JavascriptKoenView Answer on Stackoverflow
Solution 8 - JavascriptV.VillacisView Answer on Stackoverflow
Solution 9 - JavascriptBenjineerView Answer on Stackoverflow
Solution 10 - Javascripthuang botaoView Answer on Stackoverflow
Solution 11 - JavascriptRobView Answer on Stackoverflow
Solution 12 - JavascriptstackdaveView Answer on Stackoverflow
Solution 13 - JavascriptKatinka HesselinkView Answer on Stackoverflow
Solution 14 - JavascriptAlessander FrançaView Answer on Stackoverflow
Solution 15 - JavascriptLakshan HettiarachchiView Answer on Stackoverflow
Solution 16 - JavascriptAndre GoulartView Answer on Stackoverflow
Solution 17 - JavascriptSamuel QuirozView Answer on Stackoverflow
Solution 18 - JavascriptKartik BhargavView Answer on Stackoverflow