390

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

21 Answers 21

598

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

module.exports = {
    rules: {
        'no-console': 'off',
    },
};
Sign up to request clarification or add additional context in comments.

15 Comments

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 .
it works (for me) when the file is called .eslintrc.json
Alternatively, you can write "rules": {"no-console": "off"} to be less cryptic. "warn" and "error" being the other 2 options.
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.
Does not work in vue-cli 3: see answer stackoverflow.com/a/53333105/150370
|
231

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

4 Comments

How to config my .eslintrc, please tell me?
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.
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.
Sometimes, such changes require long-lasting development processes relying on it to be restarted to reload the config in memory, including nuxt-ts, scripts/commands like vite dev or npm run watch, or those which may start live development servers with hot module reload (HMR) features.
152

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",
      // more rules ....
    }
  },
  // the rest of your config ...
}

6 Comments

Thanks, as @markasoftware solution does not work if you come here for a cue-cli project.
package.json isn't the only way to do this. Separate config files are also a standar.
Which package.json file?
the one in the root folder @MaxRocket
|
67

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

Comments

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

2 Comments

console msgs still print in production environment
how would this work if .eslintrc is in JSON not regular js file?
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.

2 Comments

Needs to be JSON, not a JavaScript Object
.eslintrc.js is a JS file, so it does not need to be JSON.
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.

2 Comments

Even better, just run this handy script on all your files: find . -name '*.js' -exec gawk -i inplace 'NR==1{print "/* eslint-disable */"} {print}' {} \;
15

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

Comments

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

Comments

10

2018 October,

just do:

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

the anothers answer with

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

does not work !

Comments

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

1 Comment

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

1 Comment

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

Comments

4

You should add one rule and add your env:

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

you can add other envs.

Comments

4

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

"rules": {
    ...
    "no-console": [
      "error",
      {
        "allow": [
          "log",
          "error"
        ]
      }
    ],
    ...
  },

Comments

2

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

Comments

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

Comments

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.

Comments

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

Comments

-1

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

Comments

-6

Use Window Object

window.console.log("..")

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.