javascript 定义未定义(ESLint)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46038027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 06:24:25  来源:igfitidea点击:

Define is not defined (ESLint)

javascriptrequirejseslint

提问by El Mac

I keep getting an ESLint error 'define' is not defined. (no-undef). I believe, I could just define defineglobally, but shouldn't this be supported natively?

我不断收到 ESLint 错误'define' is not defined. (no-undef)。我相信,我可以在define全局范围内定义,但这不应该被本地支持吗?

A code example using define:

使用定义的代码示例:

define([], function () { // Error here!
    'use strict';
    ....

This is my eslintrc.json:

这是我的 eslintrc.json:

{
    "env": {
        "shared-node-browser": true,
        "commonjs": true
    },
    "plugins": ["requirejs"],
    "extends": ["eslint:recommended"],
    "rules": {
        "indent": [
            "error",
            "tab"
        ],
        "linebreak-style": [
            "error",
            "windows"
        ],
        "quotes": [
            "error",
            "single"
        ],
        "semi": [
            "error",
            "always"
        ],
        "requirejs/no-invalid-define": 2,
        "requirejs/no-multiple-define": 2,
        "requirejs/no-named-define": 2,
        "requirejs/no-commonjs-wrapper": 2,
        "requirejs/no-object-define": 1
    }
}

回答by Louis

In your .eslintrc.jsonset:

在你的.eslintrc.json集合中:

"env": {
    "amd": true
},

When the "amd" environment is turned on, eslint registers globals for defineand require.

当“amd”环境打开时,eslint 会为define和注册全局变量require

I would also turn off the "commonjs"environment unless you really are mixing both AMD and CommonJS in the same code base.

我也会关闭"commonjs"环境,除非你真的在同一个代码库中混合使用 AMD 和 CommonJS。