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

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

The answer is


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


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

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

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

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

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

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.


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.


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 */

You should add one rule and add your env:

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

you can add other envs.


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. :)


Use Window Object

window.console.log("..")


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.


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

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);

2018 October,

just do:

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

the anothers answer with

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

does not work !


Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to node.js

Hide Signs that Meteor.js was Used Querying date field in MongoDB with Mongoose SyntaxError: Cannot use import statement outside a module Server Discovery And Monitoring engine is deprecated How to fix ReferenceError: primordials is not defined in node UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac internal/modules/cjs/loader.js:582 throw err DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server Please run `npm cache clean`

Examples related to sublimetext3

Sublime text 3. How to edit multiple lines? How to open remote files in sublime text 3 Eslint: How to disable "unexpected console statement" in Node.js? Why do Sublime Text 3 Themes not affect the sidebar? How to Install Sublime Text 3 using Homebrew How to change background and text colors in Sublime Text 3 80-characters / right margin line in Sublime Text 3 What is the default font of Sublime Text? Column/Vertical selection with Keyboard in SublimeText 3 How to compile and run C in sublime text 3?

Examples related to sublime-text-plugin

Eslint: How to disable "unexpected console statement" in Node.js? Comparing the contents of two files in Sublime Text

Examples related to eslint

How to fix missing dependency warning when using useEffect React Hook? ESLint not working in VS Code? No restricted globals eslint: error Parsing error: The keyword 'const' is reserved Disable eslint rules for folder React eslint error missing in props validation Expected linebreaks to be 'LF' but found 'CRLF' linebreak-style ESLint Parsing error: Unexpected token Turning off eslint rule for a specific file Eslint: How to disable "unexpected console statement" in Node.js?