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

问题描述 投票:0回答:10

我已经安装了括号,目前正在使用变量和函数。所有输出在控制台中都可以正常工作,但是我在编辑器中不断收到此错误。

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

我该如何解决这个问题?

javascript adobe-brackets
10个回答
105
投票

no-undef
规则寻找未定义的变量,没有对环境和全局变量的任何初始假设(例如
console
)。

您可以通过在

console
中添加
browser
和/或
node
环境来指定您处于
.eslintrc
确实存在的环境中:

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

规则文档中的更多信息


7
投票

只是有评论:

/*global console*/

作为 .js 文件的第一行,然后有:

// eslint-disable-line no-console

console.log("");

线

这将使错误消失!


4
投票

既然你有它作为Capitol

C
,我猜小编认为你正在寻找一个功能或类。尝试将它从
Console.log()
降低到
console.log("john won...")
看看是否有效。


4
投票

我假设它来自 no-console 规则,它不允许调用

console
对象的方法。

在设计为在浏览器中执行的 JavaScript 中,它是 被认为是避免在

console
上使用方法的最佳实践。这样的 消息被认为是用于调试目的,因此不是 适合运送给客户。一般来说,使用
console
的调用应该 在投入生产之前被剥离。

此规则的正确代码示例:

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

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

作为解决方案,您可以将其添加到

.eslintrc

中的规则集中
rules: {
    'no-console': 'off'
}

3
投票

这个也让我发疯。您可以编辑 Brackets 的配置 JSON。它将从左侧排水沟中删除错误图标:

{
  "brackets-eslint.gutterMarks": false
}

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


1
投票

在规则对象中添加'no-console'是不活动的


0
投票

Amaury Liet 的答案适用于 ESLint 中的旧配置系统;它不适用于 v8.21.0 中引入的新配置文件(这将在 v9 中成为强制性的)。

这是解决这个问题的现代方法:

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

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

思路是使用

globals
包,传给新配置文件中的
languageOptions.globals
设置选项
eslint.config.js
.


0
投票

使用 eslint 的 new config system(使用

eslint.config.js
文件的),您应该使用以下代码片段:

import globals from "globals";

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

这已在以下博客中进行了描述:ESLint 的新配置系统,第 2 部分


-1
投票

我觉得你应该写“

console.log
”而不是“
Console.log
”。它应该是小写的。


-3
投票

这似乎是一个特例,但删除并重新创建 .eslintrc 文件对我来说解决了这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.