简体   繁体   中英

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?

The no-undef rule looks out for undefined variable, without any initial assumption on the environment and the global variables ( console for instance).

You can specify that you are in an environment where console indeed exists, by adding browser and/or node envs in your .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.

In JavaScript that is designed to be executed in the browser, it's considered a best practice to avoid using methods on 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.

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

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. Try lowering it from Console.log() to console.log("john won...") and see if that works.

This one was driving me crazy as well. You can edit Brackets' config 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

just to have comment:
/*global console*/

as the first line of your .js file, and then have:
// eslint-disable-line no-console

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

this will make the error go away!

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

I think you should write " console.log " instead of " Console.log ". It should be lowecase.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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