370

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"
};
Liam
  • 27,717
  • 28
  • 128
  • 190
Jean Y.C. Yang
  • 4,382
  • 4
  • 18
  • 27

21 Answers21

583

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

module.exports = {
    rules: {
        'no-console': 'off',
    },
};
markasoftware
  • 12,292
  • 8
  • 41
  • 69
  • 2
    Well, according to the official github page for the eslint plugin (https://github.com/roadhump/SublimeLinter-eslint#i-want-to-use-global-eslintrc-config), 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 .` – markasoftware Dec 11 '15 at 03:38
  • It remains the same problem. http://i.imgur.com/nIvrUih.png This is also useless. – Jean Y.C. Yang Dec 11 '15 at 03:44
  • 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? – markasoftware Dec 11 '15 at 03:50
  • My file is `.eslintrc.js` not `.eslintrc`, is it correct? – Jean Y.C. Yang Dec 11 '15 at 03:55
  • @JeanYang yeah, that's an issue. Name it just `.eslintrc`, and then it should work in sublime just fine. – markasoftware Dec 11 '15 at 03:59
  • If i rename it to `.eslintrc`, the console throws an error. The console mistook it as a YAML file. http://i.imgur.com/ffavJJT.png – Jean Y.C. Yang Dec 11 '15 at 04:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97575/discussion-between-markasoftware-and-jean-yang). – markasoftware Dec 11 '15 at 04:05
  • 12
    it works (for me) when the file is called .eslintrc.json – AlexWien Nov 16 '16 at 20:26
  • 18
    Alternatively, you can write `"rules": {"no-console": "off"}` to be less cryptic. `"warn"` and `"error"` being the other 2 options. – Chunky Chunk Feb 05 '17 at 19:27
  • 1
    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. – Colin D Bennett Jun 28 '17 at 15:55
  • @ColinDBennett i said .js in my answer, not sure what the problem is. – markasoftware Jun 28 '17 at 18:55
  • this syntax won't work for a `.js` file, thats `.json` – Petros Kyriakou Feb 06 '18 at 17:14
  • it will now @PetrosKyriakou! – markasoftware Feb 06 '18 at 18:12
  • 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. – Lonnie Best May 28 '18 at 05:38
  • 4
    Does not work in **vue-cli 3**: see answer https://stackoverflow.com/a/53333105/150370 – German Latorre Jul 11 '19 at 11:38
211

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 */
Nick F
  • 9,781
  • 7
  • 75
  • 90
Exception
  • 8,111
  • 22
  • 85
  • 136
  • 2
    How to config my `.eslintrc`, please tell me? – Jean Y.C. Yang Dec 11 '15 at 03:56
  • 20
    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`. – Jonathan Brizio Nov 24 '18 at 23:56
  • 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. – Gael Sep 29 '21 at 08:37
150

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"
    },
    ...
GiorgosK
  • 7,647
  • 2
  • 29
  • 26
57

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
binici
  • 775
  • 7
  • 9
42

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,
  },
Frank Spin
  • 1,463
  • 15
  • 23
18

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.

dipenparmar12
  • 3,042
  • 1
  • 29
  • 39
V.Villacis
  • 864
  • 1
  • 7
  • 19
16

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.

Jose M Bel
  • 169
  • 1
  • 2
  • Even better, just run this handy script on all your files: `find . -name '*.js' -exec gawk -i inplace 'NR==1{print "/* eslint-disable */"} {print}' {} \;` – user234461 Dec 21 '17 at 10:51
  • Rules in docs: configuration https://eslint.org/docs/user-guide/configuring#configuring-rules, rules: https://eslint.org/docs/rules/ – Denis Dec 09 '19 at 14:40
13

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);
Koen
  • 3,626
  • 1
  • 34
  • 55
11

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
  }
};
Benjineer
  • 1,530
  • 18
  • 22
10

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"
  ]
}
huang botao
  • 405
  • 6
  • 13
  • 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`. – swiesend Feb 07 '19 at 22:37
9

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

Rob
  • 216
  • 3
  • 9
  • 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**. – Nicke Manarin Jul 31 '20 at 12:53
8

2018 October,

just do:

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

the anothers answer with

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

does not work !

stackdave
  • 6,655
  • 9
  • 37
  • 56
5

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"
    },
   ...
  },
Katinka Hesselink
  • 3,961
  • 4
  • 20
  • 26
4

You should add one rule and add your env:

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

you can add other envs.

Alessander França
  • 2,697
  • 2
  • 29
  • 52
2

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

1

To just allow console.error()

warn the consoles for the debug proposes.

Add other console types in the array if needed.

module.exports = {
// ...
  rules: {
     // ...
    "no-console": ["warn", { "allow": ["error"] }],
  },
// ...
};

0

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.

Andre Goulart
  • 528
  • 2
  • 20
0

Just a workaround to avoid using the eslint-disable-next-line no-console, or adding rule in .eslintrc, or getting an Eslint warning. You can create a tempotary variable and call it instead.

const cons = console;
cons.log('My message here'); // <- won't see warning here
technik
  • 1,009
  • 11
  • 17
0

Alternatively, you can restrict only some methods on the console object with:

"rules": {
    ...
    "no-console": [
      "error",
      {
        "allow": [
          "log",
          "error"
        ]
      }
    ],
    ...
  },
gildniy
  • 3,528
  • 1
  • 33
  • 23
-1

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

Samuel Quiroz
  • 191
  • 2
  • 5
-5

Use Window Object

window.console.log("..")

Kartik Bhargav
  • 345
  • 3
  • 4
  • This answers the question succinctly and should not be downvoted. In my case, I either have to edit an `.env` file then rebuild or use a comment `// eslint-disable-next-line no-console`. Both of which are more tedious than this approach. I can simply use this for quick testing then remove when I'm done. Thank you! – DoloMike Jul 14 '22 at 16:25