Eslint:如何在Node.js中禁用“意外的控制台语句”?

我使用了Sublime Text 3的eslint,并且正在编写gulpfile.js

 /*eslint-env node*/ var gulp = require('gulp'); gulp.task('default', function(){ console.log('default task'); }); 

但eslint不断显示错误:“错误:意外的控制台语句。(无控制台)” eslint错误

我在这里find官方文档 ,但我仍然不知道如何禁用它。

 /*eslint-env node*/ var gulp = require('gulp'); /*eslint no-console: 2*/ gulp.task('default', function(){ console.log('default task'); }); 

也行不通。

我的崇高文本3插件:SublimeLinter和SublimeLinter-contrib-eslint。


**编辑**这是我的.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" }; 

在您的文件的目录中创build一个.eslintrc.js,并在其中放入以下内容:

 { "rules":{ "no-console":0 } } 

编辑:

你也可以做"no-console": "off" ,而不是0 ,使其更具可读性。

你应该更新eslintconfiguration文件来解决这个问题。 否则,您可以像下面那样临时启用或禁用eslint检查控制台

 /* eslint-disable no-console */ console.log(someThing); /* eslint-enable no-console */ 

更好的select是根据节点环境使console.log和debugger语句有条件显示。

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

如果您在本地项目下安装eslint,则应该有一个目录/ node_modules / eslint / conf /,并在该目录下有一个eslint.json文件。 您可以编辑该文件并修改值为“off”的“无控制台”条目(尽pipe也支持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", .... 

我希望这个“configuration”可以帮助你。

我使用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 } };