【问题标题】:Environment key "es2021" is unknown环境密钥“es2021”未知
【发布时间】:2021-09-08 06:55:19
【问题描述】:

很抱歉重复这个问题,但我是菜鸟,不明白如何解决这个问题。如果不难,请详细描述如何解决这个问题。

我正在使用 2017 教科书创建我的学习项目,并且该代码中没有此类错误。由于我对如此复杂的项目缺乏经验,我自己无法找到问题所在。

gulpfile

const gulp = require ('gulp') ;
const babel = require('gulp-babel');
const eslint = require('gulp-eslint');

// Задание для gulp - инструмент сборки.
gulp.task('default', async function ()
{
    // Запуск ESLint
    gulp.src(["es6/**/*.js", "public/es6/**/*.js"])
        .pipe(eslint())
        .pipe(eslint.format());
    // Указание для node.js
    // Указание кода источника для сборки.
    gulp.src("es6/**/*.js")
        // Транспортировка(pipe) файлов исходного кода в Babel, который преобразует их из ЕSб в ES5.
        .pipe(babel())
        // На завершающем этапе откомпилированный код ES5 транспортируется по назначению, в каталог dist.
        .pipe(gulp.dest("dist"));

    // Указание для браузера.
    // Указание кода источника для сборки.
    gulp.src("public/es6/**/*.js")
        // Вызов транскриптора для кода.
        .pipe(babel())
        //На завершающем этапе откомпилированный код ESS транспортируется по назначению, в каталог dist.
        .pipe(gulp.dest("public/dist"));
});

包装:

{
      "name": "javascriptproject",
      "version": "1.0.0",
      "description": "myProject",
      "main": "main.js",
      "directories": {
        "test": "test"
      },
      "dependencies": {
        "gulp-eslint": "^6.0.0",
        "save-dev": "0.0.1-security",
        "underscore": "^1.13.1"
      },
      "devDependencies": {
        "@babel/core": "^7.14.6",
        "@babel/preset-env": "^7.14.7",
        "@babel/preset-react": "^7.14.5",
        "babel-preset-es2015": "^6.24.1",
        "eslint": "^7.29.0",
        "eslint-plugin-react": "^7.24.0",
        "gulp": "^4.0.2",
        "gulp-babel": "^8.0.0"
      },
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "repository": {
        "type": "git",
        "url": "D:\\JavaScriptProject"
      },
      "keywords": [
        "my",
        "first",
        "project"
      ],
      "author": "me",
      "license": "ISC"
    }

.eslintrc:

module.exports = {
    "env": {
        "browser": true,
        "es2021": true,
        "node": true
    },
    "extends": [
        "eslint:recommended",
        "plugin:react/recommended"
    ],
    "parserOptions": {
        "ecmaFeatures": {
            "jsx": true
        },
        "ecmaVersion": 12,
        "sourceType": "module"
    },
    "plugins": [
        "react"
    ],
    "rules": {
    }
};

【问题讨论】:

  • 只需尝试替换 "es2021": true, 以在您的 modules.export 中使用 es2020
  • 我试过并得到了这个... D:\JavaScriptProject\es6\test.js 0:0 错误解析错误:无效的 ecmaVersion
  • 您在ecmaversion 上获得智能感知吗?选择并按 Ctrl+Space,你有选项吗?如果有,请选择es2020(如果有)。
  • 我明白了!并感谢帮助我。你向我展示了正确的方式。
  • 将 es2021 -> es2020 和 ecmaVersion 从 12 更改为 11,如果不兼容则进一步减少

标签: javascript gulp eslint gulp-eslint


【解决方案1】:

经过一些改动后我得到了它

.eslintrc

  1. 我改了"es2021": true, into "es2020": true,
  2. 收到错误Parsing error: Invalid ecmaVersion 后,我更改了"ecmaVersion": 12into "ecmaVersion": 11
  3. 最后一个我检查了https://github.com/yannickcr/eslint-plugin-react#recommend 并找到了如何“启用”所有配置的解决方案,其中包括这部分中的每个可用规则:
 "extends": [
        "eslint:recommended",
        "plugin:react/recommended",
        "eslint:all", // just added this line
        "plugin:react/all" // and this line
    ],

我不确定这是否真的有效,但看起来是这样的:

D:\JavaScriptProject\public\es6\test.js
   1:1   error  'use strict' is unnecessary inside of modules                                    strict
   1:1   error  Strings must use doublequote                                                     quotes
   1:14  error  Expected linebreaks to be 'LF' but found 'CRLF'                                  linebreak-style
   2:1   error  Expected space or tab after '//' in comment                                      spaced-comment
   2:40  error  Expected linebreaks to be 'LF' but found 'CRLF'                                  linebreak-style
   3:18  error  Expected linebreaks to be 'LF' but found 'CRLF'                                  linebreak-style
   4:6   error  Expected linebreaks to be 'LF' but found 'CRLF'                                  linebreak-style
   5:10  error  Extra space after key 'subject'                                                  key-spacing
   5:10  error  Unquoted property 'subject' found                                                quote-props
   5:20  error  Strings must use doublequote                                                     quotes
   5:34  error  Object properties must go on a new line                                          object-property-newline
   5:34  error  Unquoted property 'verb' found                                                   quote-props
   5:40  error  Strings must use doublequote                                                     quotes
   5:46  error  Object properties must go on a new line                                          object-property-newline
   5:46  error  Unquoted property 'object' found                                                 quote-props
   5:46  error  Expected object keys to be in ascending order. 'object' should be before 'verb'  sort-keys
   5:54  error  Strings must use doublequote                                                     quotes
   5:63  error  Expected linebreaks to be 'LF' but found 'CRLF'                                  linebreak-style
   6:10  error  Extra space after key 'subject'                                                  key-spacing
   6:10  error  Unquoted property 'subject' found                                                quote-props
   6:20  error  Strings must use doublequote                                                     quotes
   6:33  error  Object properties must go on a new line                                          object-property-newline
   6:33  error  Unquoted property 'verb' found                                                   quote-props
   6:39  error  Strings must use doublequote                                                     quotes
   6:47  error  Object properties must go on a new line                                          object-property-newline
   6:47  error  Unquoted property 'object' found                                                 quote-props
   6:47  error  Expected object keys to be in ascending order. 'object' should be before 'verb'  sort-keys
   6:55  error  Strings must use doublequote                                                     quotes
   6:63  error  Unexpected trailing comma                                                        comma-dangle
   6:64  error  Expected linebreaks to be 'LF' but found 'CRLF'                                  linebreak-style
   7:7   error  Expected linebreaks to be 'LF' but found 'CRLF'                                  linebreak-style
   8:1   error  Expected linebreaks to be 'LF' but found 'CRLF'                                  linebreak-style
   9:42  error  Expected linebreaks to be 'LF' but found 'CRLF'                                  linebreak-style
  10:1   error  Expected a function expression                                                   func-style
  10:13  error  Missing space before function parentheses                                        space-before-function-paren
  10:38  error  Expected linebreaks to be 'LF' but found 'CRLF'                                  linebreak-style
  11:1   error  Opening curly brace does not appear on the same line as controlling statement    brace-style
  11:1   error  Block must be padded by blank lines                                              padded-blocks
  11:2   error  Expected linebreaks to be 'LF' but found 'CRLF'                                  linebreak-style
  12:5   error  Unexpected console statement                                                     no-console
  12:51  error  Expected linebreaks to be 'LF' but found 'CRLF'                                  linebreak-style
  13:1   error  Block must be padded by blank lines                                              padded-blocks
  13:2   error  Expected linebreaks to be 'LF' but found 'CRLF'                                  linebreak-style
  14:1   error  Comments should not begin with a lowercase character                             capitalized-comments
  14:26  error  Expected linebreaks to be 'LF' but found 'CRLF'                                  linebreak-style
  15:1   error  Expected space(s) after "for"                                                    keyword-spacing
  15:9   error  Identifier name 's' is too short (< 2)                                           id-length
  15:9   error  's' is never reassigned. Use 'const' instead                                     prefer-const
  15:24  error  Expected linebreaks to be 'LF' but found 'CRLF'                                  linebreak-style
  16:1   error  Opening curly brace does not appear on the same line as controlling statement    brace-style
  16:1   error  Block must be padded by blank lines                                              padded-blocks
  16:2   error  Expected linebreaks to be 'LF' but found 'CRLF'                                  linebreak-style
  17:12  error  Expected linebreaks to be 'LF' but found 'CRLF'                                  linebreak-style
  18:1   error  Block must be padded by blank lines                                              padded-blocks
  18:2   error  Newline required at end of file but not found                                    eol-last

✖ 110 problems (110 errors, 0 warnings)
  100 errors and 0 warnings potentially fixable with the `--fix` option.

【讨论】:

    猜你喜欢
    • 2021-04-04
    • 1970-01-01
    • 2017-09-01
    • 1970-01-01
    • 2019-04-29
    • 2017-10-04
    • 2014-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多