【问题标题】:Parsing Error The Keyword import is Reserved (SublimeLinter-contrib-eslint)解析错误关键字导入被保留(SublimeLinter-contrib-eslint)
【发布时间】:2016-06-30 08:36:00
【问题描述】:

我的 eslint 有问题,它给了我 [Parsing Error The keyword import is reserve] 这仅在 sublime 中发生,在 atom 编辑器中运行良好。我有 eslint

.eslintrc.js

module.exports = {
    "extends": "airbnb",
    "plugins": [
        "react"
    ]
};

package.json

{
  "name": "paint",
  "version": "0.0.0",
  "description": "paint on the browser",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "paint",
    "javascript"
  ],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "browserify": "^11.2.0",
    "eslint": "^2.2.0",
    "eslint-config-airbnb": "^2.1.1",
    "eslint-plugin-react": "^3.11.2",
    "gulp-babel": "^5.2.1",
    "gulp-clean": "^0.3.1",
    "gulp-stylus": "^2.2.0",
    "vinyl-source-stream": "^1.1.0"
  }
}

【问题讨论】:

    标签: javascript parsing sublimetext eslint


    【解决方案1】:

    将此添加到您的 .eslintrc.json(以前为 .eslintrc)的根目录

    "parser": "babel-eslint"
    

    并确保运行:

    npm install babel-eslint --save-dev
    

    【讨论】:

    • 这对我也有用,谢谢。如果它对任何人有帮助,我的整个 .eslint 文件是 {"parser": "babel-eslint"}
    • 对我来说,文件必须重命名为.eslintrc,其内容为{"parser" : "babel-eslint"}?
    • 无需更改解析器。做"parserOptions": { "sourceType": "module" }就够了
    • @nathancahill 谢谢。但是 babel-eslint 还有其他更普遍的好处吗?
    • 截至 2021 年 7 月,{ "parser": "@babel/eslint-parser" }
    【解决方案2】:

    解决“The keyword import is reserved”错误的eslint选项是parserOptions.sourceType。将其设置为 "module" 允许使用 import 关键字。

    .eslintrc

    {
        "parserOptions": {
            "sourceType": "module"
        }
    }
    

    文档: https://eslint.org/docs/user-guide/configuring#specifying-parser-options

    【讨论】:

    【解决方案3】:

    花了 30 分钟 - 尝试了所有解决方案,但力不从心,所以分享这个。

    问题出现在 new react app 和 Visual Studio Code 中,即使此时 - 2020 年 4 月也是如此。

    1. 在根文件夹(package.json 旁边,或/src/ 目录旁边)创建一个文件.eslintrc.js
    2. 将下面的内容粘贴到.eslintrc.js
    3. 重新启动编辑器,例如 VS Code。
    4. 现在我可以看到真正的错误,而不是那些虚假的导入/导出错误。

    .eslintrc.js文件内容:

    module.exports = {
      env: {
        commonjs: true,
        node: true,
        browser: true,
        es6: true,
        jest: true,
      },
      extends: ["eslint:recommended", "plugin:react/recommended"],
      globals: {},
      parser: "babel-eslint",
      parserOptions: {
        ecmaFeatures: {
          jsx: true,
        },
        ecmaVersion: 2018,
        sourceType: "module",
      },
      plugins: ["react", "import", "react-hooks"],
      ignorePatterns: ["node_modules/"],
      rules: {},
      settings: {
        react: {
          version: "latest", // "detect" automatically picks the version you have installed.
        },
      },
    };
    

    希望对您有所帮助。

    【讨论】:

    • 很高兴知道:)
    • @ManoharReddyPoreddy谢谢!您知道我们是否需要安装任何依赖项才能使其正常工作?
    • @batatop 我想,只是 eslint 安装。但是,上面我也安装了 react.js,我遇到了问题。
    • 重启你的编辑器,比如 VS Code。这就是我的全部!谢谢
    【解决方案4】:

    问题是我在全局和本地安装了 eslint,导致 SublimeLinter-contrib-eslint 不一致。我全局卸载了 eslint,SublimeLinter 正在运行。

    【讨论】:

    • 谢谢。 IMO 这个问题可能比 SublimeLinter 更普遍。
    • 源于模块类型的语法。 CommonJS 模块使用'require'。
    【解决方案5】:

    关闭 VS 代码并重新打开它对我有用...

    【讨论】:

      【解决方案6】:

      不确定,但尝试将文件重命名为 .eslintrc 并使用

      {
        "extends": "airbnb",
        "plugins": ["react"]
      };
      

      还要确保您已安装所需的软件包。 github.com/airbnb/javascript

      【讨论】:

      • 我解决了错误,问题是我在全球和本地都有 eslint。谢谢
      • 奇怪的是 .eslintrc.js 到 .eslintrc 为我修复了它,奇怪。谢谢!
      • jfyi - 这不适用于 VS Code,无论是否安装了全局 eslint。
      • 我花了一段时间才意识到我将文件命名为 eslintrc.js 而不是 .eslintrc.js(没有前导点)。
      【解决方案7】:

      不过,接受的答案有效,不再处于维护状态,新建议的方法是改用 mono repo 中的版本。

      安装

      $ npm install eslint @babel/core @babel/eslint-parser --save-dev
      # or
      $ yarn add eslint @babel/core @babel/eslint-parser -D
      

      .eslintrc.js

      module.exports = {
        parser: "@babel/eslint-parser",
      };
      

      Reference

      【讨论】:

        【解决方案8】:

        我在一个流星项目中也遇到了这个错误,我可以通过将 sourceType 设置为“模块”来解决它 更多细节可以在 Eslint 文档中找到: http://eslint.org/docs/user-guide/configuring#specifying-parser-options

        【讨论】:

        • jfyi - 这不适用于 VS Code,复制粘贴到 parserOptions > sourceType。
        【解决方案9】:

        这个配置对我有用。 (我正在使用 create-react-app 但适用于任何 eslint 项目)


        .eslintrc (create file in root if it doesnt exist)

        {
            "rules": {
              "jsx-a11y/anchor-is-valid": [ "error", {
                "components": [ "Link" ],
                "specialLink": [ "to" ]
              }]
            },
            "parserOptions": {
                "sourceType": "module",
                "ecmaVersion": 2015
            }
          }
        

        【讨论】:

          【解决方案10】:

          我在创建 vue 项目时发现了这个问题(使用的编辑器:Visual Code)

          安装 babel-eslint 包 -> npm install babel-eslint

          创建 .eslintrc.js 文件并添加以下代码

          module.exports = { 根:是的, 解析器选项:{ 'sourceType': '模块', 解析器:'babel-eslint'
          } }

          => npm run serve,该错误将像魔术一样解决。

          【讨论】:

          • 或者,尝试不使用 'sourceType' module.exports = { root: true, parserOptions: { parser: 'babel-eslint' } }
          【解决方案11】:

          在启用 eslint 的情况下,在 typescript react-native 项目中创建 js 文件时也会出现同样的问题。

          将文件类型从 js 更改为 ts 解决了该问题。

          此外,添加 .eslintrc.js 文件(如先前答案中所述)解决了该问题,而无需将文件类型从 js 更改为 ts。

                 module.exports = {
                    parser: "@babel/eslint-parser",
                 };
          

          【讨论】:

            【解决方案12】:

            将 ecmaVersion 添加到 .eslintrc.json 解决了问题

            {
                "ecmaVersion": 2015,
                "extends": [
                    "eslint:recommended",
                    "plugin:react/recommended"
                ]
            }
            

            【讨论】:

              猜你喜欢
              • 2017-07-31
              • 2020-07-20
              • 2020-12-02
              • 2017-02-11
              • 2020-01-22
              • 1970-01-01
              • 1970-01-01
              • 2017-04-10
              • 2023-01-11
              相关资源
              最近更新 更多