Javascript Eslint:如何在 Node.js 中禁用“意外的控制台语句”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34215526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Eslint: How to disable "unexpected console statement" in Node.js?
提问by Jean Y.C. Yang
I'm using eslint with Sublime Text 3 and I am writing gulpfile.js
.
我正在将 eslint 与 Sublime Text 3 一起使用,并且正在编写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 一直显示错误:“错误:意外的控制台语句。(无控制台)”
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.
我的 Sublime Text 3 插件:SublimeLinter 和 SublimeLinter-contrib-eslint。
Here's my .eslintrc.js
file:
这是我的.eslintrc.js
文件:
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"
};
回答by markasoftware
Create a .eslintrc.js in the directory of your file, and put the following contents in it:
在你的文件目录下创建一个 .eslintrc.js ,并将以下内容放入其中:
module.exports = {
rules: {
'no-console': 'off',
},
};
回答by Exception
You should update eslint config file to fix this permanently. Else you can temporarily enable or disable eslint check for console like below
您应该更新 eslint 配置文件以永久修复此问题。否则,您可以临时启用或禁用控制台的 eslint 检查,如下所示
/* eslint-disable no-console */
console.log(someThing);
/* eslint-enable no-console */
回答by GiorgosK
For vue-cli 3open package.json
and under section eslintConfig
put no-console
under rules
and restart dev server (npm run serve
or yarn serve
)
对于vue-cli 3open 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"
},
...
回答by Frank Spin
A nicer option is to make the display of console.log and debugger statements conditional based on the node environment.
更好的选择是根据节点环境使 console.log 和调试器语句的显示有条件。
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,
},
回答by binici
The following works with ESLint in VSCode if you want to disable the rule for just one line.
如果您只想禁用一行的规则,则以下适用于 VSCode 中的 ESLint。
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
回答by Jose M Bel
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):
如果你在本地项目下安装 eslint,你应该有一个目录 /node_modules/eslint/conf/ 并且在该目录下有一个文件 eslint.json。您可以编辑文件并修改值为“off”的“no-console”条目(尽管也支持 0 值):
"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.
我希望这个“配置”可以帮助你。
回答by Benjineer
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:
我正在使用 Ember.js,它生成一个名为.eslintrc.js
. 添加"no-console": 0
到规则对象为我完成了这项工作。更新后的文件如下所示:
module.exports = {
root: true,
parserOptions: {
ecmaVersion: 6,
sourceType: 'module'
},
extends: 'eslint:recommended',
env: {
browser: true
},
rules: {
"no-console": 0
}
};
回答by Koen
If you just want to disable the rule once, you want to look at Exception's answer.
如果您只想禁用一次规则,则需要查看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);
回答by huang botao
in my vue
project i fixed this problem like this :
在我的vue
项目中,我像这样解决了这个问题:
vim package.json
...
"rules": {
"no-console": "off"
},
...
ps : package.json is a configfile in the vue project dir, finally the content shown like this:
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"
]
}
回答by Rob
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):
如果根据文档配置 package.json 后仍然遇到问题(如果您选择使用 package.json 来跟踪而不是单独的配置文件):
"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. :)
它仍然不适合您,不要忘记您需要返回命令行并再次执行 npm install。:)