Webpack with React : Error saying i need a loader but babelrc and webpack.config.js are set with appropriate plugins

170 Views Asked by At

i'm struggling with webpack and react for days now. You're my only hope.

I have a message error i cant fix. I saw other thread dealing with this issue but it seems i've set up everything well.

here is the error message in dev mode :

    Compiled with problems:
×
ERROR in ./src/index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: .presets[0][1] must be an object, false, or undefined
    at Object.loadPartialConfigAsync (/*****/****/******/******/*****/react_props/task_0/dashboard/node_modules/@babel/core/lib/config/index.js:34:85)
    at Object.loader /*****/****/******/******/*****/react_props/task_0/dashboard/node_modules/babel-loader/lib/index.js:109:30)
    at Object.<anonymous> (/*****/****/******/******/*****/react_props/task_0/dashboard/node_modules/babel-loader/lib/index.js:39:12)

and the one in build mode :

ERROR in ./src/index.js 9:2
Module parse failed: Unexpected token (9:2)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| const root = ReactDOM.createRoot(document.getElementById('root'));
| root.render(
>   <React.StrictMode>
|     <App />
|   </React.StrictMode>

Here is my webpack config :

const path = require('path');
const ReactRefreshPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const TerserPlugin = require("terser-webpack-plugin");

const isDevelopment = process.env.NODE_ENV !== 'production';

module.exports = {
  mode: isDevelopment ? 'development' : 'production',
  entry: path.resolve(__dirname, '../src/index.js'),
  output: {
    path: path.resolve(__dirname, './dist'),
    filename: 'bundle.js',
  },
  resolve: {
    extensions: ['', '.js', '.jsx'],
  },
  devServer: {
    static: {
      directory: path.join(__dirname, "./dist"),
      serveIndex: true,
    },
    open: true,
    hot: true,
  },
  devtool: 'inline-source-map',
  module: {
    rules: [
      {
        test: /\.css$/i,
        use: ["style-loader", "css-loader"],
      },
      {
        test: /\.(png|svg|jpg|jpeg|gif)$/i,
        use: [
          'file-loader',
          {
            loader: 'image-webpack-loader',
            options: {
              bypassOnDebug: true,
              disable: true,
            },
          },
        ],
      },
      {
        test: /\.[jt]sx?$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: [
              ["@babel/preset-env", "@babel/preset-react"]
            ],
            plugins: ['@babel/plugin-proposal-class-properties']
          }
        }
      },
    ],
  },
  plugins: [
    isDevelopment && new ReactRefreshPlugin(),
    new HtmlWebpackPlugin({
      title: 'Holberton Dashboard',
      template: './dist/index.html',
      inject: false
    }),
    new CleanWebpackPlugin()
  ],
  optimization: {
    minimize: true,
    minimizer: [
      new TerserPlugin(),
    ],
  }
};

My babelrc :

{
    "presets": [
        ["@babel/preset-react", {"runtime": "automatic"}],
        [
            "@babel/preset-env",
            {
                "targets": {
                    "browsers": "last 2 versions"
                },
                "modules": false,
                "loose": false
            }
        ]
    ],
    "plugins": [
        "react-hot-loader/babel"
    ],
    "env": {
        "test": {
            "plugins": [
                "transform-es2015-modules-commonjs"
            ]
        }
    }
}

and my package.json :

{
  "name": "dashboard",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "jest",
    "webpack-dev-server": "webpack-dev-server --config ./config/webpack.config.js",
    "start-dev": "webpack-dev-server --open",
    "dev": "webpack serve --mode development",
    "build": "webpack --mode production"
  },
  "config": {
    "webpack": "webpack.config.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-script": "^2.0.5"
  },
  "devDependencies": {
    "@babel/core": "^7.22.11",
    "@babel/preset-env": "^7.22.14",
    "@babel/preset-react": "^7.22.5",
    "@cfaester/enzyme-adapter-react-18": "^0.7.1",
    "@pmmmwh/react-refresh-webpack-plugin": "^0.5.11",
    "babel": "^6.23.0",
    "babel-cli": "^6.26.0",
    "babel-loader": "^9.1.3",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "babel-plugin-transform-es2015-modules-commonjs": "^6.26.2",
    "clean-webpack-plugin": "^4.0.0",
    "css-loader": "^6.8.1",
    "enzyme": "^3.11.0",
    "file-loader": "^6.2.0",
    "html-webpack-plugin": "^5.5.3",
    "image-webpack-loader": "^8.1.0",
    "jest": "^29.6.4",
    "react-refresh": "^0.14.0",
    "style-loader": "^3.3.3",
    "webpack": "^5.88.2",
    "webpack-cli": "^5.1.4",
    "webpack-dev-server": "^4.15.1"
  },
  "jest": {
    "setupFiles": [
      "<rootDir>/config/setupTests.js"
    ],
    "moduleNameMapper": {
      "\\.(css)$": "<rootDir>/__mocks__/styleMock.js",
      "\\.(jpg)$": "<rootDir>/__mocks__/fileMock.js",
      "\\.(png)$": "<rootDir>/__mocks__/fileMock.js"
    }
  }
}

Thanks for you help and looking at my files.

1

There are 1 best solutions below

0
Keith On

Reading the docs -> https://babeljs.io/docs/presets

For specifying no options, these are all equivalent:

{
  "presets": [
    "presetA", // bare string
    ["presetA"], // wrapped in array
    ["presetA", {}] // 2nd argument is an empty options object
  ]
}

But your presets are ->

presets: [
  ["@babel/preset-env", "@babel/preset-react"]
],

So what your saying is use "@babel/preset-env" with options "@babel/preset-react" which is of course incorrect.

So what your likely after is ->

presets: [
  "@babel/preset-env", 
  "@babel/preset-react"
],
// OR
presets: [
  ["@babel/preset-env"], 
  ["@babel/preset-react"]
],
// OR even
presets: [
  ["@babel/preset-env", {}], 
  ["@babel/preset-react", {}]
],
``