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

Jea*_*ang 229 javascript node.js sublimetext3 eslint sublime-text-plugin

我正在使用与Sublime Text 3的eslint,我正在写作gulpfile.js.

/*eslint-env node*/
var gulp = require('gulp');

gulp.task('default', function(){
    console.log('default task');
});
Run Code Online (Sandbox Code Playgroud)

但是eslint一直显示错误:"错误:意外的控制台声明.(无控制台)" 夹板错误

我在这里找到了官方文档,但我仍然不知道如何禁用它.

/*eslint-env node*/
var gulp = require('gulp');

/*eslint no-console: 2*/
gulp.task('default', function(){
    console.log('default task');
});
Run Code Online (Sandbox Code Playgroud)

也行不通.

我的Sublime Text 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"
};
Run Code Online (Sandbox Code Playgroud)

mar*_*are 395

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

module.exports = {
    rules: {
        'no-console': 'off',
    },
};
Run Code Online (Sandbox Code Playgroud)

  • 或者,你可以写"规则":{"no-console":"off"}`不那么神秘.``警告'`和`'错误'`是另外两个选项. (11认同)
  • 当文件被调用时,它(对我而言).eslintrc.json (9认同)
  • 在 **vue-cli 3** 中不起作用:请参阅答案 /sf/answers/3733317381/ (4认同)
  • 好吧,根据eslint插件的官方github页面(https://github.com/roadhump/SublimeLinter-eslint#i-want-to-use-global-eslintrc-config),将.eslintrc文件放入您的项目中文件夹应该可以解决问题...继续进行调试,建议您尝试从命令行运行eslint。只需安装未安装的node.js,然后从控制台/命令提示符下运行`npm install eslint`,然后在控制台/命令提示符下导航到您的项目文件夹,然后运行`eslint。 (2认同)
  • 这个对我有用。但是应该重新启动服务器。:) (2认同)

Exc*_*ion 118

您应该更新eslint配置文件以永久修复此问题.否则,您可以暂时启用或禁用以下控制台的eslint检查

/* eslint-disable no-console */
console.log(someThing);
/* eslint-enable no-console */
Run Code Online (Sandbox Code Playgroud)

  • 不必同时添加这两行。仅将`console.log`放在前面,以下例外就足够了:`eslint-disable-next-line no-console`。 (4认同)
  • 如何配置我的`.eslintrc`,请告诉我? (2认同)

Gio*_*osK 85

对于VUE-CLI 3开放package.json和第eslintConfig穿上no-consolerules并重新启动开发服务器(npm run serveyarn serve)

...
"eslintConfig": {
    ...
    "rules": {
      "no-console": "off"
    },
    ...
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,因为如果您来这里是为了一个提示 cli 项目,@markasoftware 解决方案将不起作用。 (2认同)

Fra*_*pin 30

一个更好的选择是根据节点环境使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,
  },
Run Code Online (Sandbox Code Playgroud)


小智 13

如果在本地项目下安装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",
    ....
Run Code Online (Sandbox Code Playgroud)

我希望这个"配置"可以帮到你.


bin*_*ici 12

如果要仅禁用一行规则,以下内容适用于VSCode中的ESLint.

要禁用下一行:

// eslint-disable-next-line no-console
console.log('hello world');
Run Code Online (Sandbox Code Playgroud)

要禁用当前行:

console.log('hello world'); // eslint-disable-line no-console
Run Code Online (Sandbox Code Playgroud)


Ben*_*eer 10

我正在使用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
  }
};
Run Code Online (Sandbox Code Playgroud)


Koe*_*oen 10

如果您只想禁用一次规则,则需要查看Exception的答案.

您可以通过仅禁用一行规则来改进此问题:

...在当前行:

console.log(someThing); /* eslint-disable-line no-console */
Run Code Online (Sandbox Code Playgroud)

......或在下一行:

/* eslint-disable-next-line no-console */
console.log(someThing);
Run Code Online (Sandbox Code Playgroud)


hua*_*tao 8

在我的vue项目中,我解决了以下问题:

vim package.json
...
"rules": {
    "no-console": "off"
},
...
Run Code Online (Sandbox Code Playgroud)

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"
  ]
}
Run Code Online (Sandbox Code Playgroud)


Rob*_*Rob 7

如果根据文档配置 package.json 后仍然遇到问题(如果您选择使用 package.json 来跟踪而不是单独的配置文件):

"rules": {
      "no-console": "off"
    },
Run Code Online (Sandbox Code Playgroud)

它仍然不适合您,不要忘记您需要返回命令行并再次执行 npm install。:)


V.V*_*cis 6

或者,您可以允许,而不是关闭“无控制台”。在.eslintrc.js文件中放入

  rules: {
    "no-console": [
     "warn",
     { "allow": ["clear", "info", "error", "dir", "trace", "log"] }
    ]
  }
Run Code Online (Sandbox Code Playgroud)

这将允许你这样做console.log,并console.clear没有引发错误等。


Kat*_*ink 5

package.json中,您将找到eslintConfig一行。您的“规则”行可以像这样进入:

  "eslintConfig": {
   ...
    "extends": [
      "eslint:recommended"
    ],
    "rules": {
      "no-console": "off"
    },
   ...
  },
Run Code Online (Sandbox Code Playgroud)


sta*_*ave 5

2018 年 10 月,

做就是了:

// tslint:disable-next-line:no-console
Run Code Online (Sandbox Code Playgroud)

别人回答

// eslint-disable-next-line no-console
Run Code Online (Sandbox Code Playgroud)

不工作!