解析错误关键字导入已保留(SublimeLinter-contrib-eslint)

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

我遇到了 eslint 的问题,它给了我 [解析错误关键字导入是保留] 这仅发生在 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
14个回答
242
投票

将此添加到您的

.eslintrc.json
(以前的
.eslintrc
)的根目录

"parser": "babel-eslint"

并确保运行:

npm install babel-eslint --save-dev

98
投票

解决“关键字导入被保留”错误的eslint选项是

parserOptions.sourceType
。将其设置为
"module"
允许使用
import
关键字。

.eslintrc

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

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


19
投票

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


14
投票

关闭 VS code 并重新打开它对我来说很有效......


7
投票

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

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

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


7
投票

接受的答案有效,但是不再维护,新建议的方法是使用 mono 存储库中的版本。

安装

$ 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",
};

参考


3
投票

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


3
投票

这个配置对我有用。 (我正在使用 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
    }
  }

2
投票

在启用 eslint 的情况下,在 TypeScript React-Native 项目中创建

js
文件时,也会出现同样的问题。

将文件类型从

js
更改为
ts
解决了问题。

此外,按照前面的答案中所述添加 .eslintrc.js 文件解决了该问题,而无需将文件类型从

js
更改为
ts

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

2
投票

即使在 2022 年 8 月的这个时候,新的 React 应用程序和 Visual Studio Code 中也出现了该问题

在根文件夹中创建文件

.eslintrc.js
将以下内容粘贴到
.eslintrc.js
重新启动编辑器,例如 VS Code。 现在我可以看到真正的错误,而不是那些假的导入/导出错误。
.eslintrc.js
文件内容:

   export const parser = "@babel/eslint-parser";

接受的答案有效,但是,新建议的方法是使用 ES6 的版本。


1
投票

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

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

1
投票

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

安装 babel-eslint 包

npm 安装 babel-eslint

创建

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

module.exports = {
    root: true,
    parserOptions: {
        'sourceType': 'module',
        parser: 'babel-eslint'   
    }
}

npm run serve
,该错误将像魔法一样被解决。


0
投票

ESlint .eslintrc.json 文件中的禁用规则。

  {
  "extends": "next",
  "rules": {
    "react/no-unescaped-entities": "off",
    "@next/next/no-page-custom-font": "off"
  }
}

0
投票

我今天在 next.js 13 上遇到了这个错误,我的 eslint 文件如下所示:

{
  "root": true,
  "settings": {
    "react": {
      "version": "detect"
    },
    "env": {
      "browser": true,
      "node": true,
      "es2020": true,
      "jest": true
    },
    "extends": [
      "eslint:recommended",
      "plugin:react/recommended",
      "plugin:react-hooks/recommended",
      "plugin:@typescript-eslint/recommended",
      "plugin:prettier/recommended"
    ],
    "parser": "@typescript-eslint/parser",
    "parserOptions": {
      "ecmaVersion": "latest",
      "sourceType": "module",
      "ecmaFeatures": {
        "jsx": true
      }
    },
    "plugins": ["react", "react-hooks", "@typescript-eslint"],
    "rules": {}
  }
}

所以我删除了这段代码,它工作正常:

"settings": {
        "react": {
          "version": "detect"
        }
© www.soinside.com 2019 - 2024. All rights reserved.