48

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.

ERROR: 'Console' is not defined (no-undef)

How do I go about fixing this?

Amaury Liet
  • 10,196
  • 3
  • 18
  • 24
Riddick
  • 493
  • 1
  • 4
  • 7

10 Answers10

109

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

Amaury Liet
  • 10,196
  • 3
  • 18
  • 24
  • Thanks! Although I don't want to commit any `console.log`'s in the app, it's nice to at least not have the error saying "console is undefined" while debugging small things. – dabyland Apr 23 '19 at 14:54
7

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!

Moritz Ringler
  • 9,772
  • 9
  • 21
  • 34
Yunjun Wang
  • 71
  • 1
  • 3
  • 1
    or you can add these two lines to disable it completely in the whole .js file: /*global console*/ /* eslint no-console: "off" */ – Yunjun Wang Aug 20 '18 at 14:21
4

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.

S.S.
  • 684
  • 8
  • 21
4

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'
}
ummahusla
  • 2,043
  • 3
  • 28
  • 42
3

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

nemanja
  • 664
  • 5
  • 16
1

add 'no-console' in rules object is inactive

liulong
  • 11
  • 2
  • 3
    Please add further details to expand on your answer, such as working code or documentation citations. – Community Sep 06 '21 at 07:21
0

Amaury Liet's answer works with the old configuration system in ESLint; it doesn't work with the new configuration file introduced in v8.21.0 (which will become mandatory in v9).

Here's the modern way of solving this problem:

// eslint.config.js
import js from "@eslint/js";
import globals from "globals";

export default [
  js.configs.recommended,
  {
    files: ["src/**/*.js"],
    languageOptions: {
      globals: {
        ...globals.node,
      },
    },
  // ...
}

The idea is to use the globals package and pass it to the languageOptions.globals setting option in the new configuration file eslint.config.js.

Paul Razvan Berg
  • 16,949
  • 9
  • 76
  • 114
0

With eslint's new config system (the one that uses the eslint.config.js file), you should use the following code snipet:

import globals from "globals";

export default [
    {
        files: ["**/*.js"],
        languageOptions: {
            globals: {
                ...globals.browser,
            }
        }
    }
];

This has been described in the following blog: ESLint's new config system, Part 2

-1

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

dominikjosch
  • 119
  • 1
  • 5
-3

This might seem like a special case, but deleting and recreating the .eslintrc file fixed this issue for me.

seveneights
  • 425
  • 5
  • 15