【发布时间】:2022-05-21 23:12:39
【问题描述】:
在 Visual Studio 代码中我得到
Parsing error: The keyword 'import' is reserved.
您建议采取哪些措施来纠正此错误?
0。一个最小的、完整的、可验证的例子
我在下面提供了我的 .eslintrc.json 和 package.json 文件。
但是,它们可能不足以重现错误。
所以这里有一个链接
a zip file containing all the necessary project files.
要在本地安装项目,请运行 npm install。 - 这可能
大约需要5-9分钟。
1
然后npm start 应该在默认的网络浏览器中打开项目。
2
当我这样做并点击 F12 时,我没有收到任何错误,但有两个 浏览器控制台中的警告。
警告是:
-
'unUsedArgument' is defined but never used. Allowed unused args must match /^_/u no-unused-vars,和 -
'unUsedVariable' is assigned a value but never used. Allowed unused vars must match /^_/u no-unused-vars.
1。解析错误:关键字'import'被保留
标题中的错误与我选择的文字无关
编辑器。
这很容易通过从命令行运行 ESLint 来确认。
npx eslint src
2。视觉工作室代码? – 其他文本编辑器或 IDE:s?
错误,Parsing error: The keyword 'import' is reserved,也
当我在 VS Code 中打开 App.js 时出现。
虽然我使用的是 Visual Studio Code,但我还是邀请答案(和
其他文本编辑器和 IDE:s 的讨论)。
请注意——除了通过npm 正确安装 ESLint——你
还必须为您的特定安装插件/扩展
集成开发环境 (IDE)。
就我而言,我使用official VS Code ESLint extension。
3
不过,这里的重点显然应该放在npm 命令和
Node.js 包的安装。
3。我的可重现示例的配置文件
package.json:
{
"name": "parsing-error",
"version": "0.1.0",
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.4.1",
"@testing-library/user-event": "^7.2.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version"
]
}
}
.eslintrc.json:
{
"rules": {
"no-unused-vars": [
"warn",
{
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_"
}
]
}
}
4。这个问题是重复的吗?
有人问我:我的问题是否与
Parsing Error The Keyword import is Reserved
(SublimeLinter-contrib-eslint) 重复?
我相信我的问题不是该问题的重复。
我的问题package.json的起源来自于
Create React App 通过命令npx create-react-app <the-name-of-my-app>.
另一个问题的package.json 缺少
react-scripts
每个 Create React App 必须具有的 npm 包。
另一个问题显然不是关于 Create React App,而
我的问题是。
4
4a。 ESLint 如何集成到 Create React App 中?
正如this answer 解释的那样,
创建 React App 直接依赖于 react-scripts 包。
react-scripts 包又依赖于
eslint-config-react-app 包,
这又取决于@babel/eslint-parser 包。
我将在下面回到后者。
4b。其他问题的任何答案都解决了我的问题吗?
在the Community FAQ index for Stack Overflow 中有一个链接到 what I can do when I think my question is not a duplicate,然后链接到 另一个post that (implicitly) defines when a question is a duplicate of another question。
总之,判断一个问题是否重复的关键标准是 任何其他问题的答案都为我的问题提供了解决方案 自己的问题。
所以关键问题是是否有这样的答案。
首先,
the highest voted answer 是
已过时,因为它建议安装
the deprecated babel-eslint package。
该软件包的作者明确指示使用
后继包@babel/eslint-parser 而不是
babel-eslint.
安装已弃用的babel-eslint 包会更麻烦
在我的 Create React App 示例中存在问题,因为
@babel/eslint-parser 已安装。
我不能接受同时安装两个不同的建议
ESLint Parser 的版本(其中一个已被弃用)。
The accepted answer 不是 与我的情况相关,因为我仅在本地安装了 ESLint (不是全局的)。
对于所有其他答案,我尝试了建议的解决方案 我的可重复的例子,一一。 他们中的大多数都导致了与我的不同错误消息,但是 他们都没有解决我的问题——与两个自我回答相反 我已经在下面发布了。
虽然这两个问题中的错误消息是相同的 - 它们发生的原因明显不同。 我的结论是我的问题不是重复 Parsing Error The Keyword import is Reserved (SublimeLinter-contrib-eslint).
5。其他错误报告
在下面列表的参考文献 18-24 中,我链接了一些以前的报告 这里的错误 - 或类似的错误。 其中一些链接(问题)可能与这里的问题有关, 但可能不是全部。
参考文献
- What does MCVE mean – minimal, complete and verifiable example?
- Zip file containing the needed project files
- To install ESLint globally rather than locally (using
npm install eslint --global) is not recommended - VS Code ESLint extension
- In VS Code, add
"eslint.nodePath": "C:\\Program Files\\nodejs",tosettings.json - Parsing Error The Keyword import is Reserved (SublimeLinter-contrib-eslint)
- Create React App
- The react-scripts npm package
- How is ESLint integrated into Create React App?
eslint-config-react-app@babel/eslint-parser- The Community FAQ index for Stack Overflow
- What can I do when I think my question's not a duplicate?
- A post that (implicitly) defines when a question is a duplicate of another question
- The highest voted answer of other question
- The deprecated
babel-eslintpackage - Installing ESLint globally might be a bad idea
- Parsing error: The keyword 'export' is reserved
- Parsing error: The keyword 'import' is reserved
- ERROR: Parsing Error: The keyword 'const' is reserved
- eslint: error Parsing error: The keyword 'const' is reserved
- Setting up Airbnb ESLint with React and Webpack
- Parsing error: The keyword 'enum' is reserved
- ESLint Parsing error: Unexpected token
1 由于it is recommended to install ESLint locally, 我假设这里的所有读者都这样做。
2 就我而言,Google Chrome 版本 98.0.4758.102,64 位。 在 Windows 10 上运行。
3
除了安装扩展,我还有
added "eslint.nodePath": "C:\\Program Files\\nodejs",
to my (user) settings.json
VS 代码中的文件。没有其他变化。
4 我在我的问题中添加了create-react-app 标签。
【问题讨论】:
-
感谢您的提问。 – 不。我的问题不是与Parsing Error The Keyword import is Reserved (SublimeLinter-contrib-eslint) 的重复。例如,我的问题中的
package.json(间接)依赖于@babel/eslint-parser,另一个问题中的package.jsonnot 对它有任何依赖。我打算明天重写我的问题来解释这一点。
标签: create-react-app reactjs npm eslint create-react-app