62

I am trying to use ESLint in WebStrom, but it doesn't work and displays an error:

ESLint: Parsing error: This experimental syntax requires enabling one of the following parser plugin(s): 'jsx, flow, typescript' (2:9).

Here is my .eslintrc and package.json settings. What should I do for fixing the error?

package.json

{
  "name": "raonair-frontend",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "eslint-config-react-app": "^6.0.0",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-router-dom": "^5.2.0",
    "react-scripts": "4.0.3",
    "web-vitals": "^1.0.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "prepare": "husky install"
  },
  "parser": "babel-eslint",
  "eslintConfig": {
    "extends": [
      "react-app",
      "airbnb",
      "plugin:flowtype/recommended"
    ],
    "plugins": [
      "flowtype"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@babel/eslint-parser": "^7.15.0",
    "@babel/eslint-plugin": "^7.14.5",
    "@babel/plugin-syntax-jsx": "^7.14.5",
    "eslint": "^7.32.0",
    "eslint-config-airbnb": "^18.2.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-flowtype": "^5.9.0",
    "eslint-plugin-import": "^2.23.4",
    "eslint-plugin-jsx-a11y": "^6.4.1",
    "eslint-plugin-react": "^7.24.0",
    "eslint-plugin-react-hooks": "^4.2.0",
    "husky": "^7.0.1",
    "prettier": "^2.3.2"
  }
}

.eslintrc

{
  "env": {
    "browser": true,
    "node": true
  },
  "extends": [
    "airbnb",
    "airbnb/hooks",
    "prettier"
  ],
  "parser": "@babel/eslint-parser",
  "parserOptions": {
    "sourceType": "module",
    "allowImportExportEveryWhere": false,
    "ecmaFeatures": {
      "globalReturn": false,
      "jsx": true
    },
    "ecmaVersion": 2020,
    "babelOptions": {
      "configFile": "./babel.config.js"
    }
  },
  "plugins": [
    "jsx-a11y",
    "react-hooks",
    "@babel/",
    "flowtype",
    "import"
  ],
  "rules": {
    "import/no-anonymous-default-export": "off",
    "import/no-extraneous-dependencies": "off",
    "import/order": [
      "error",
      {
        "groups": [
          "builtin",
          "external",
          "internal",
          "parent",
          "sibling"
        ],
        "newlines-between": "always",
        "alphabetize": {
          "order": "asc",
          "caseInsensitive": false
        }
      }
    ],
    "import/prefer-default-export": "off",
    "indent": [
      "error",
      2,
      {
        "SwitchCase": 1
      }
    ],
    "jsx-a11y/anchor-is-valid": "off",
    "no-console": "error",
    "no-unused-vars": "error",
    "react/jsx-props-no-spreading": "off",
    "react/react-in-jsx-scope": "off"
  },
  "settings": {
    "import/resolver": {
      "typescript": {}
    },
    "insert_final_newline": true
  }
}
1
  • You haven't provided us with any other project files than .eslintrc(.json) and package.json. I was not able to reproduce the error message Parsing error: This experimental syntax requires enabling one of the following parser plugin(s): 'jsx, flow, typescript' (2:9) but instead got Line 0: Parsing error: Cannot find module './babel.config.js'. Commented Feb 24, 2022 at 13:59

5 Answers 5

58
  1. Install @babel/preset-react in dev dependencies.

  2. Add this in .eslintrc file

     ...
     "parser": "@babel/eslint-parser",
     "parserOptions": {
        ...
        "babelOptions": {
           "presets": ["@babel/preset-react"]
        },
       }
     ...
    

Source: https://ffan0811.medium.com/error-debugging-this-experimental-syntax-requires-enabling-one-of-the-following-parser-plugin-s-22946599a0a4

Sign up to request clarification or add additional context in comments.

1 Comment

Since "parser": "@babel/eslint-parser", is already in .eslintrc(.json), there is no need to add it.
43

To expand on Ceroy's answer (source):

  1. Install dependencies needed to enable babel to properly parse jsx

yarn add @babel/preset-react -D

or

npm install --save-dev @babel/preset-react

  1. Configure Babel

.babelrc file in the project root:

{
    "presets": [
        "@babel/preset-react"
    ]
}
  1. Specify parser in .eslintrc.json (so your console and VSCode will use the same one) - full eslintrc shown here:
{
    "parser": "@babel/eslint-parser",
    "settings": {
        "react": {
            "version": "detect"
        }
    },
    "env": {
        "browser": true,
        "es2021": true
    },
    "extends": [
        "plugin:react/recommended",
        "standard",
        "plugin:react/jsx-runtime",
        "plugin:testing-library/react",
        "plugin:jest/all"
    ],
    "parserOptions": {
        "sourceType": "module"
    },
    "rules": {}
}

Note: .eslintrc.json can also be created via npm init @eslint/config or by using the ESLint extension in VSCode. The init will give you more options to choose from (such as TypeScript support)

  1. Try it out in the console: npx eslint --fix .

  2. Check VSCode (with the ESLint extension)

Bonus:

ESLint Plugin Settings for VSCode (autofix on save, Jest aware, babel parser etc. - in global VSCode settings.json):

    ...
    "eslint.alwaysShowStatus": true,
    "eslint.format.enable": true,
    "eslint.lintTask.enable": true,
    "eslint.options": {
        "parser": "@babel/eslint-parser"
    },
    "editor.codeActionsOnSave": {
        "source.fixAll.eslint": true
    },
    "eslint.validate": [
        "javascript"
    ]
    ...

Full package.json:

{
  "name": "okta-test",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^12.1.0",
    "@testing-library/user-event": "^13.2.1",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-router": "^5.2.1",
    "react-router-dom": "^5.3.0",
    "react-scripts": "4.0.3",
    "web-vitals": "^2.1.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@babel/eslint-parser": "^7.15.7",
    "@babel/preset-react": "^7.14.5",
    "eslint": "^7.32.0",
    "eslint-config-standard": "^16.0.3",
    "eslint-plugin-babel": "^5.3.1",
    "eslint-plugin-import": "^2.24.2",
    "eslint-plugin-jsx": "^0.1.0",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-promise": "^5.1.0",
    "eslint-plugin-react": "^7.26.0"
  }
}

Comments

1

I have faced the same issue but for me what work out is like below. I have added the screenshot of error and steps to resolve it

Step 1 : Install the @babel/preset-react dependancy if already not available in your package.json file

yarn add @babel/preset-react  

package.json file for reference

{
  "name": "animationapp",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "start": "expo start --dev-client",
    "android": "expo run:android",
    "ios": "expo run:ios",
    "web": "expo start --web",
    "test": "jest",
    "lint": "eslint . --ext .js,.jsx,.ts,.tsx"
  },
  "dependencies": {
    "@react-native-community/masked-view": "^0.1.11",
    "@react-navigation/drawer": "^6.6.5",
    "@react-navigation/native": "^6.1.9",
    "@rnmapbox/maps": "^10.0.15",
    "expo": "~49.0.11",
    "expo-splash-screen": "~0.20.5",
    "expo-status-bar": "~1.6.0",
    "react": "18.2.0",
    "react-native": "0.72.4",
    "react-native-copilot": "^3.2.1",
    "react-native-gesture-handler": "^2.13.3",
    "react-native-reanimated": "3.5.2",
    "react-native-safe-area-context": "^4.7.3",
    "react-native-screens": "^3.26.0",
    "react-native-svg": "^13.14.0"
  },
  "devDependencies": {
    "@babel/core": "^7.20.0",
    "@babel/eslint-parser": "^7.23.3",
    "@babel/preset-react": "^7.23.3",
    "@testing-library/jest-native": "^5.4.3",
    "@testing-library/react-native": "^12.4.0",
    "babel-eslint": "^10.1.0",
    "eslint": "^8.53.0",
    "eslint-config-airbnb": "^19.0.4",
    "eslint-plugin-import": "^2.29.0",
    "eslint-plugin-jsx-a11y": "^6.8.0",
    "eslint-plugin-react": "^7.33.2",
    "jest": "^29.7.0",
    "react-test-renderer": "^18.2.0"
  },
  "private": true
}

Step 2 : Add .eslintrc.js file with following options aas mentioned below the important line for me is like to add below jsx support

parserOpts: {
        plugins: ["jsx"]
      }

After adding this into .eslintrc.js file it looks like below

module.exports = {
  parser: "@babel/eslint-parser",
  parserOptions: {
    requireConfigFile: false,
    babelOptions: {
      babelrc: false,
      configFile: false,
      // your babel options
      presets: ["@babel/preset-env"],
      parserOpts: {
        plugins: ["jsx"]
      }
    },
  },
};

Step 3 : Add .eslintignore for ignoring files and folders from linting and add .babelrc file as well

.babelrc

{
    "presets": [
        "@babel/preset-react"
    ]
}

.eslintignore file

build/*.js
config/*.js
coverage/*.js
coverage/*
jest/*.js
__tests__/*
__tests__/*.js

Step 4 : Here before and after adding above setups it works for me running yarn lint without any error

enter image description here

enter image description here

Comments

0

I went through this error. My problem was the version issue of eslint-config-standard.

So my solution is eslint-config-standard version downgrade 17.0.0 => 16.0.3

"eslint-config-standard": "^17.0.0" => "^16.0.3"

Comments

0
module.exports = {
  root: true,
  extends: '@react-native',
  overrides: [
    {
      files: ['*.js', '*.jsx', '*.ts', '*.tsx'],
      parserOptions: {
        requireConfigFile: false,
      },
    },
  ],
  parserOpts: {
    plugins: ["jsx"]
  }
};
Use this hopefully it works

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.