简体   繁体   English

错误:'控制台未定义。 [no-undef] - 括号

[英]ERROR: 'console is not defined. [no-undef] - Brackets

I've installed brackets and currently playing around with variables and functions.我已经安装了括号,目前正在使用变量和函数。 All the outputs work fine in the console, however I keep getting this error in the editor.所有输出在控制台中都可以正常工作,但是我在编辑器中不断收到此错误。

错误:“控制台”未定义(无-undef)

How do I go about fixing this?我如何解决这个问题 go?

The no-undef rule looks out for undefined variable, without any initial assumption on the environment and the global variables ( console for instance). no-undef规则查找未定义的变量,对环境和全局变量(例如console )没有任何初始假设。

You can specify that you are in an environment where console indeed exists, by adding browser and/or node envs in your .eslintrc :您可以指定你是在一个环境console确实存在,加入browser和/或node在ENVS .eslintrc

  env: {
    browser: true,
    node: true,
  },

More info in the rule doc规则文档中的更多信息

I assume it's coming from no-console rule, which disallows calls to methods of the console object.我假设它来自无控制台规则,该规则不允许调用console对象的方法。

In JavaScript that is designed to be executed in the browser, it's considered a best practice to avoid using methods on console .在旨在在浏览器中执行的 JavaScript 中,避免在console上使用方法被认为是最佳实践。 Such messages are considered to be for debugging purposes and therefore not suitable to ship to the client.此类消息被视为用于调试目的,因此不适合发送给客户端。 In general, calls using console should be stripped before being pushed to production.通常,使用console调用应该在推送到生产之前被剥离。

Examples of correct code for this rule:此规则的正确代码示例:

/*eslint no-console: "error"*/

// custom console Console.log("Hello world!");

As a solution, you can add this to your set of rules in .eslintrc作为解决方案,您可以将其添加到.eslintrc的规则.eslintrc

rules: {
    'no-console': 'off'
}

Since you have it as a Capitol C , I would guess that the editor thinks you're looking for a function or class.由于您将它作为 Capitol C ,我猜编​​辑器认为您正在寻找一个函数或类。 Try lowering it from Console.log() to console.log("john won...") and see if that works.尝试将它从Console.log()降低到console.log("john won...")看看是否有效。

This one was driving me crazy as well.这个也让我发疯了。 You can edit Brackets' config JSON.您可以编辑 Brackets 的配置 JSON。 It will remove the error icon from the left gutter:它将从左侧装订线中删除错误图标:

{
  "brackets-eslint.gutterMarks": false
}

Reference: https://github.com/brackets-userland/brackets-eslint/blob/master/README.md参考: https : //github.com/brackets-userland/brackets-eslint/blob/master/README.md

just to have comment:只是有评论:
/*global console*/

as the first line of your .js file, and then have:作为 .js 文件的第一行,然后有:
// eslint-disable-line no-console

at the line of the console.log("");console.log("");

this will make the error go away!这将使错误消失!

在规则对象中添加“无控制台”处于非活动状态

I think you should write " console.log " instead of " Console.log ".我认为你应该写“ console.log ”而不是“ Console.log ”。 It should be lowecase.它应该是小写的。

删除并重新创建 .eslintrc 文件为我解决了这个问题。

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

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