In Visual Studio Code I get
Parsing error: The keyword 'import' is reserved.
What actions would you recommend to remedy this error?
0. A minimal, complete and verifiable example
I provide my .eslintrc.json and package.json files below.
However, they will likely not be sufficient to reproduce the error.
So here is a link to
a zip file containing all the necessary project files.
To locally install the project, run npm install. – This may
take about 5-9 minutes.
1
Then npm start should open the project in the default web browser.
2
When I do this and hit F12, I get no errors but two warnings in the console of the browser.
The warnings are:
'unUsedArgument' is defined but never used. Allowed unused args must match /^_/u no-unused-vars, and'unUsedVariable' is assigned a value but never used. Allowed unused vars must match /^_/u no-unused-vars.
1. Parsing error: The keyword 'import' is reserved
The error in the title has nothing to do with my choice of text
editor.
This is easy to confirm by running ESLint from the command line.
npx eslint src
2. Visual Studio Code? – Other text editors or IDE:s?
The error, Parsing error: The keyword 'import' is reserved, also
shows up when I open App.js in VS Code.
Although I am using Visual Studio Code, I invite answers (and
discussions) of other text editors and IDE:s as well.
Note that – in addition to installing ESLint correctly via npm – you
also have to install a plugin/extension for your specific
integrated development environment (IDE).
In my case, I use the official VS Code ESLint extension.
3
Still, the focus here should clearly be on the npm command and on
the installation of Node.js packages.
3. Configuration files of my reproducible example
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. Is this question a duplicate?
I have been asked : is my question here a duplicate of
Parsing Error The Keyword import is Reserved
(SublimeLinter-contrib-eslint)?
I believe my question is not a duplicate of that question.
The origin of the package.json of my question comes from a
Create React App via the command
npx create-react-app <the-name-of-my-app>.
The package.json of the other question is missing the
react-scripts
npm package which every Create React App must have.
The other question is clearly not about Create React App, whereas
my question is.
4
4a. How is ESLint integrated into Create React App?
As this answer explains, a
Create React App depends directly on the react-scripts package.
The react-scripts package in turn depends on the
eslint-config-react-app package,
which in turn depends on the @babel/eslint-parser package.
I will come back to the latter below.
4b. Do any of the answers of the other question solve my question?
In the Community FAQ index for Stack Overflow there is a link to what I can do when I think my question is not a duplicate, which in turn links to another post that (implicitly) defines when a question is a duplicate of another question.
In summary, the key criterion for if a question is a duplicate is if any of the answers to the other question provides a solution to my own question.
So the key question is if there is such an answer.
First of all,
the highest voted answer is
obsolete in that it suggests to install
the deprecated babel-eslint package.
The authors of that package explicitly instruct to use the
successor package @babel/eslint-parser instead of
babel-eslint.
Installing the deprecated babel-eslint package would be even more
problematic in my Create React App example, since
@babel/eslint-parser is already installed.
I cannot accept a suggestion to simultaneously install two different
versions of the ESLint Parser (of which one is deprecated).
The accepted answer is not relevant in my case, as I installed ESLint only locally (not globally).
For all the other answers I tried the suggested solutions on my reproducible example, one by one. Most of them resulted in a different error message than mine, but none of them solved my question – contrary to the two self-answers I have posted below.
Although the error messages are identical in the two questions – the reasons for why they occur are obviously significantly different. I conclude that my question is not a duplicate of Parsing Error The Keyword import is Reserved (SublimeLinter-contrib-eslint).
5. Other reports of the error
In references 18-24 of the list below, I link some previous reports of the error here – or similar errors. Some of those links (questions) are likely related to the issue here, but possibly not all of them.
References
- 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 Since it is recommended to install ESLint locally, I assume all readers here do so.
2 In my case Google Chrome Version 98.0.4758.102, 64-bit. Running on Windows 10.
3
Apart from installing the extension, I have
added "eslint.nodePath": "C:\\Program Files\\nodejs",
to my (user) settings.json
file in VS Code. No other changes.
4 I have added the create-react-app tag to my question.