简体   繁体   English

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

[英]Eslint: How to disable "unexpected console statement" in Node.js?

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 一直显示错误:“错误:意外的控制台语句。(无控制台)” 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"
};

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

You should update eslint config file to fix this permanently.您应该更新 eslint 配置文件以永久修复此问题。 Else you can temporarily enable or disable eslint check for console like below否则,您可以临时启用或禁用控制台的 eslint 检查,如下所示

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

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 )对于vue-cli 3,打开package.json并在eslintConfig部分eslintConfigno-console放在rules下并重新启动开发服务器( npm run serveyarn serve

...
"eslintConfig": {
    ...
    "rules": {
      "no-console": "off"
    },
    ...

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

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

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.如果你在本地项目下安装 eslint,你应该有一个目录 /node_modules/eslint/conf/ 并且在该目录下有一个文件 eslint.json。 You could edit the file and modify "no-console" entry with the value "off" (although 0 value is supported too):您可以编辑文件并修改值为“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.我希望这个“配置”可以帮助你。

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

I'm using Ember.js which generates a file named .eslintrc.js .我正在使用 Ember.js,它生成一个名为.eslintrc.js的文件。 Adding "no-console": 0 to the rules object did the job for me."no-console": 0到 rules 对象为我完成了这项工作。 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
  }
};

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:

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

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

Alternatively instead of turning 'no-console' off, you can allow.或者,您可以允许,而不是关闭“无控制台”。 In the .eslintrc.js file put.eslintrc.js文件中放入

  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.这将允许您执行console.logconsole.clear等操作而不会抛出错误。

In package.json you will find an eslintConfig line.package.json 中,您会找到eslintConfig行。 Your 'rules' line can go in there like this:您的“规则”行可以像这样进入:

  "eslintConfig": {
   ...
    "extends": [
      "eslint:recommended"
    ],
    "rules": {
      "no-console": "off"
    },
   ...
  },

2018 October, 2018 年 10 月,

just do:做就是了:

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

the anothers answer with别人回答

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

does not work !不工作!

You should add one rule and add your env:您应该添加一个规则并添加您的环境:

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

you can add other envs.您可以添加其他环境。

Putting this in the .eslintrc.js file that is at the project location worked for me 将其放在项目位置的.eslintrc.js文件中对我有用

module.exports = {
    rules: {
        'no-console': 0
    }
};

在“规则”,“无控制台”中:[假,“日志”,“错误”]

My 2 cents contribution:我的 2 美分贡献:

Besides removing the console warning (as shown above), it's best to remove yours logs from PROD environments (for security reasons).除了删除控制台警告(如上所示)之外,最好从 PROD 环境中删除您的日志(出于安全原因)。 The best way I found to do so, is by adding this to nuxt.config.js我发现这样做的最好方法是将它添加到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.工作原理:Nuxt 已经使用 terser 作为缩小器。 This config will force terser to ignore/remove all console logs commands during compression.此配置将强制 terser 在压缩期间忽略/删除所有控制台日志命令。

make sure that the name of the folder that the flutter project is in. Doesn't have spaces.确保颤振项目所在文件夹的名称。没有空格。 that was my error那是我的错误

使用窗口对象

window.console.log("..")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM