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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36002226/
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-08-23 18:30:16  来源:igfitidea点击:

Parsing Error The Keyword import is Reserved (SublimeLinter-contrib-eslint)

javascriptparsingsublimetexteslint

提问by pedro luis

I have a problem with eslint, it gives me [Parsing Error The keyword import is reserve] this is only occur in sublime, in atom editor work well. I have eslint

我的 eslint 有问题,它给了我 [解析错误关键字导入保留] 这仅发生在 sublime 中,在 atom 编辑器中运行良好。我有 eslint

.eslintrc.js

.eslintrc.js

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

package.json

包.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"
  }
}

采纳答案by pedro luis

The problem was i had installed eslint globally and locally, causing inconsistencies in SublimeLinter-contrib-eslint. I uninstalled eslint globally and SublimeLinter is working.

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

回答by Iman Mohamadi

Add this to the root of your .eslintrc

将此添加到 .eslintrc 的根目录中

"parser": "babel-eslint"

and make sure to run:

并确保运行:

npm i babel-eslint --save-dev

回答by pedro luis

The eslint option that solves the "The keyword import is reserved" error is parserOptions.sourceType. Setting it to "module"allows the importkeyword to be used.

解决“保留关键字导入”错误的 eslint 选项是parserOptions.sourceType. 将其设置为"module"允许使用import关键字。

.eslintrc

.eslintrc

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

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

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

回答by the

Not sure about it but try to rename your file to .eslintrcand just use

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

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

Also be sure you have the required packages installed. github.com/airbnb/javascript

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

回答by SPM

i also got this error in a meteor project and i could solved it setting sourceType to "module" more details can be found in Eslint docs: http://eslint.org/docs/user-guide/configuring#specifying-parser-options

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

回答by Nitin Jadhav

This config worked for me. (I am using create-react-appbut applicable to any eslint project)

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



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

.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
    }
  }

回答by Bhupinder

I found this issue while creating the vue project (Used Editor: Visual Code)

我在创建 vue 项目时发现了这个问题(Used Editor: Visual Code)

Install babel-eslint package -> npm install babel-eslint

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

Create the .eslintrc.js file and add below code

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

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

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

=> npm run serve, that error will be resolved like magic.

=> npm run serve,这个错误会像魔法一样解决。

回答by Manohar Reddy Poreddy

Spent 30 mins - trying all solutions but dint work, so sharing this one.

花了 30 分钟 - 尝试了所有解决方案,但没有用,所以分享这个。

The issue is seen with new react app, and in Visual Code, even at this time - Apr 2020.

即使在此时 - 2020 年 4 月,也可以在new react app和 中看到该问题Visual Code

  1. Create a file .eslintrc.jsin the root folder (beside package.json, or beside /src/directory)
  2. Paste below contents in .eslintrc.js
  3. Restart your editor, like VS Code.
  4. Now I can see real errors, instead of those fake import/export errors.
  1. .eslintrc.js在根文件夹中创建一个文件(在package.json,或在/src/目录旁边)
  2. 将以下内容粘贴到 .eslintrc.js
  3. 重启你的编辑器,比如 VS Code。
  4. 现在我可以看到真正的错误,而不是那些虚假的导入/导出错误。

.eslintrc.jsfile contents:

.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.
    },
  },
};

Hope that helps.

希望有帮助。

回答by JulienRioux

Closing VS code and re-open it does the trick for me...

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