3

I created a simple app a few weeks ago using create-react-app almost immediately I started getting this error in VSCode about it not being happy about something Babel related...which I ignored. My app works, I'm able to build and deploy to Heroku. And ESLint is also working. But the error keeps popping up in VSCode which has lead me down this rabbit hole of Babel and ESLint nightmares.

The error I was getting was regarding babel-eslint which I actually didn't have installed despite it being the parser named in my .eslintrc.json config. This is what my package.json file looked like prior to me making any changes:

{
  "name": "new-rails-react-project",
  "private": true,
  "dependencies": {
    "@babel/preset-react": "^7.13.13",
    "@rails/actioncable": "^6.0.0",
    "@rails/activestorage": "^6.0.0",
    "@rails/ujs": "^6.0.0",
    "@rails/webpacker": "5.4.0",
    "axios": "^0.21.1",
    "babel-plugin-transform-react-remove-prop-types": "^0.4.24",
    "jquery": "^3.6.0",
    "prop-types": "^15.7.2",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-router-dom": "^5.2.0",
    "redbox-react": "^1.6.0",
    "webpack": "^4.46.0",
    "webpack-cli": "^3.3.12"
  },
  "version": "0.1.0",
  "devDependencies": {
    "eslint": "^7.27.0",
    "eslint-config-prettier": "^8.3.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",
    "prettier": "^2.3.0",
    "webpack-dev-server": "^3.11.2"
  },
  "scripts": {
    "start": "./bin/webpack-dev-server"
  }
}

And this is what my .eslintrc.json file looked like:

{
  "extends": [
    "eslint:recommended",
    "plugin:import/errors",
    "plugin:react/recommended",
    "plugin:jsx-a11y/recommended",
    "prettier"
  ],
  "plugins": ["react", "import", "jsx-a11y", "react-hooks"],
  "rules": {
    "react/prop-types": 0,
    "react-hooks/rules-of-hooks": "error",
    "no-console": "warn",
    "no-dupe-keys": "warn",
    "jsx-a11y/rule-name": 0,
    "jsx-a11y/anchor-has-content": "warn"
  },
  "parser": "babel-eslint",
  "parserOptions": {
    "ecmaVersion": 2018,
    "sourceType": "module",
    "ecmaFeatures": { "jsx": true }
  },
  "env": {
    "es6": true,
    "browser": true,
    "node": true
  },
  "settings": {
    "react": {
      "version": "detect"
    }
  }
}

Since babel-eslint is deprecated I added @babel/eslint-parser and cannot for the life of me figure out how to make this work. Here are my current related config files: package.json

  "name": "ne-campground-reviews",
  "private": true,
  "dependencies": {
    "@babel/plugin-transform-react-jsx": "^7.14.5",
    "@babel/preset-react": "^7.13.13",
    "@rails/actioncable": "^6.0.0",
    "@rails/activestorage": "^6.0.0",
    "@rails/ujs": "^6.0.0",
    "@rails/webpacker": "5.4.0",
    "axios": "^0.21.1",
    "babel-plugin-transform-react-remove-prop-types": "^0.4.24",
    "jquery": "^3.6.0",
    "lightbox2": "^2.11.3",
    "prop-types": "^15.7.2",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-icons": "^4.2.0",
    "react-router-dom": "^5.2.0",
    "redbox-react": "^1.6.0",
    "webpack": "^4.46.0",
    "webpack-cli": "^3.3.12"
  },
  "version": "0.1.0",
  "devDependencies": {
    "@babel/core": "^7.14.6",
    "@babel/eslint-parser": "^7.14.7",
    "@babel/eslint-plugin": "^7.14.5",
    "eslint": "^7.29.0",
    "eslint-config-prettier": "^8.3.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",
    "prettier": "^2.3.0",
    "webpack-dev-server": "^3.11.2"
  },
  "scripts": {
    "start": "./bin/webpack-dev-server"
  }
}

.eslintrc.json

{
  "extends": [
    "eslint:recommended",
    "plugin:import/errors",
    "plugin:react/recommended",
    "plugin:jsx-a11y/recommended",
    "prettier"
  ],
  "plugins": ["react", "import", "jsx-a11y", "react-hooks", "@babel"],
  "rules": {
    "react/prop-types": 0,
    "react-hooks/rules-of-hooks": "error",
    "no-console": "warn",
    "no-dupe-keys": "warn",
    "jsx-a11y/rule-name": 0,
    "jsx-a11y/anchor-has-content": "warn"
  },
  "parser": "@babel/eslint-parser",
  "parserOptions": {
    "ecmaVersion": 2018,
    "sourceType": "module",
    "ecmaFeatures": { "jsx": true },
    "requireConfigFile": false,
    "babelOptions": {
      "presets": ["@babel/preset-react"]
}
  },
  "env": {
    "es6": true,
    "browser": true,
    "node": true
  },
  "settings": {
    "react": {
      "version": "detect"
    }
  }
}

And this is babel.config.js although I don't know if this file is still needed and/or configured correctly

module.exports = function(api) {
  var validEnv = ['development', 'test', 'production']
  var currentEnv = api.env()
  var isDevelopmentEnv = api.env('development')
  var isProductionEnv = api.env('production')
  var isTestEnv = api.env('test')

  if (!validEnv.includes(currentEnv)) {
    throw new Error(
      'Please specify a valid `NODE_ENV` or ' +
        '`BABEL_ENV` environment variables. Valid values are "development", ' +
        '"test", and "production". Instead, received: ' +
        JSON.stringify(currentEnv) +
        '.'
    )
  }

  return {
    presets: [
      isTestEnv && [
        '@babel/preset-env',
        {
          targets: {
            node: 'current'
          },
          modules: 'commonjs'
        },
        '@babel/preset-react'
      ],
      (isProductionEnv || isDevelopmentEnv) && [
        '@babel/preset-env',
        {
          forceAllTransforms: true,
          useBuiltIns: 'entry',
          corejs: 3,
          modules: false,
          exclude: ['transform-typeof-symbol']
        }
      ],
      [
        '@babel/preset-react',
        {
          development: isDevelopmentEnv || isTestEnv,
          useBuiltIns: true
        }
      ]
    ].filter(Boolean),
    plugins: [
      'babel-plugin-macros',
      '@babel/plugin-syntax-dynamic-import',
      isTestEnv && 'babel-plugin-dynamic-import-node',
      '@babel/plugin-transform-destructuring',
      [
        '@babel/plugin-proposal-class-properties',
        {
          loose: true
        }
      ],
      [
        '@babel/plugin-proposal-object-rest-spread',
        {
          useBuiltIns: true
        }
      ],
      [
        '@babel/plugin-transform-runtime',
        {
          helpers: false,
          regenerator: true,
          corejs: false
        }
      ],
      [
        '@babel/plugin-transform-regenerator',
        {
          async: false
        }
      ],
      isProductionEnv && [
        'babel-plugin-transform-react-remove-prop-types',
        {
          removeImport: true
        }
      ]
    ].filter(Boolean)
  }
}

This error is what's currently popping up on things like import and module

Parsing error: Cannot find module '@babel/preset-react'
Require stack:
- /Users/maddoxgrey/Projects/ne-campground-reviews-v2/node_modules/@babel/core/lib/config/files/plugins.js
- /Users/maddoxgrey/Projects/ne-campground-reviews-v2/node_modules/@babel/core/lib/config/files/index.js
- /Users/maddoxgrey/Projects/ne-campground-reviews-v2/node_modules/@babel/core/lib/index.js
Maddox Grey
  • 61
  • 1
  • 6
  • Try `npm i @babel/preset-react` and check. – Shravan Dhar Jun 28 '21 at 18:01
  • That doesn't fix it. In my dependencies "@babel/preset-react": "^7.13.13" is listed. – Maddox Grey Jun 28 '21 at 18:13
  • I might actually be getting closer to fixing this, I'm now getting the error: `Parsing error: This experimental syntax requires enabling one of the following parser plugin(s): 'jsx, flow, typescript'` – Maddox Grey Jun 28 '21 at 21:38
  • 1
    Had the same parser error `jsx, flow, typescript` and could fix it by supplying the babel config file to the eslints configuration: in the file .eslintrc.js add `const path = require('path'); module.exports = { ..., parserOptions: { babelOptions: { configFile: path.resolve(`${__dirname}/.babelrc`) }, ...} – Marc Dec 13 '21 at 10:11
  • @Marc Consider writing your comment as an answer, this was actually a helpful solution to my problem. My error was "Parsing error: No Babel config file detected ..." – Sagiv b.g Oct 23 '22 at 09:46

0 Answers0