Eslint: How to disable "unexpected console statement" in Node.js?
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,
},
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, 2022Comments
-
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)"
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 over 8 yearsWell, 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 runeslint .
-
Jean Y.C. Yang over 8 yearsIt remains the same problem. i.imgur.com/nIvrUih.png This is also useless.
-
markasoftware over 8 yearshmm, 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 over 8 yearsMy file is
.eslintrc.js
not.eslintrc
, is it correct? -
Jean Y.C. Yang over 8 yearsHow to config my
.eslintrc
, please tell me? -
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 over 8 yearsIf i rename it to
.eslintrc
, the console throws an error. The console mistook it as a YAML file. i.imgur.com/ffavJJT.png -
markasoftware over 8 yearsLet us continue this discussion in chat.
-
AlexWien over 7 yearsit works (for me) when the file is called .eslintrc.json
-
Chunky Chunk over 7 yearsAlternatively, you can write
"rules": {"no-console": "off"}
to be less cryptic."warn"
and"error"
being the other 2 options. -
Colin D Bennett almost 7 yearsThe 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 almost 7 years@ColinDBennett i said .js in my answer, not sure what the problem is.
-
user234461 over 6 yearsEven 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 over 6 yearsthis syntax won't work for a
.js
file, thats.json
-
markasoftware over 6 yearsit will now @PetrosKyriakou!
-
Lonnie Best almost 6 yearsA .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 over 5 yearsNot 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 over 5 yearsThanks, as @markasoftware solution does not work if you come here for a cue-cli project.
-
swiesend over 5 yearsthis is helpful if one generated a vue project trough
vue-cli
orvue ui
and it contains avue.config.js
andpackage.json
. Edit thepackage.json
. -
Michael Cole about 5 yearspackage.json isn't the only way to do this. Separate config files are also a standar.
-
German Latorre almost 5 yearsDoes not work in vue-cli 3: see answer stackoverflow.com/a/53333105/150370
-
Ank over 4 yearsThanks! You saved my day.
-
Denis over 4 yearsRules in docs: configuration eslint.org/docs/user-guide/configuring#configuring-rules, rules: eslint.org/docs/rules
-
Denis over 4 yearsThanks! Rules in docs: configuration eslint.org/docs/user-guide/configuring#configuring-rules, rules: eslint.org/docs/rules
-
MaxRocket over 4 yearsWhich package.json file?
-
GiorgosK over 4 yearsthe one in the root folder @MaxRocket
-
Sanyam Jain almost 4 yearsconsole msgs still print in production environment
-
Scott Baker almost 4 yearsNeeds to be JSON, not a JavaScript Object
-
Admin almost 4 yearshow would this work if .eslintrc is in JSON not regular js file?
-
Nicke Manarin almost 4 yearsInteresting, 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 over 2 yearsThanks @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 over 2 years
.eslintrc.js
is a JS file, so it does not need to be JSON.