Search code examples
javascriptnode.jsreactjsbabeljsbabel-loader

React build error: Module build failed (from ./node_modules/babel-loader/lib/index.js):


I am following along with a tutorial and when I try to build react I get an error.

Module build failed (from ./node_modules/babel-loader/lib/index.js)
  5 | ReactDOM.render(
> 6 |     <App />,
    |     ^
  7 |     document.getElementById('root')
  8 | );

I found similar issues and I tried to fix the issue based on this but to no avail. This is my webpack.config.js:

module.exports = {
    entry: './index.js',
    output:{
        path: __dirname,
        filename: 'bundle.js'
    },
    module:{
        rules:[
            {
                test: /\.jsx?$/,
                loader: 'babel-loader',
                exclude: '/node_modules/'
            }
        ]
    }
}

Below I have package.json, the "devDependencies" are what I have found in previous solutions.

{
  "name": "rtsupport",
  "version": "1.0.0",
  "description": "Realtime support frontend",
  "main": "./index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "develop": "webpack --mode development --watch",
    "build": "webpack --mode development"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "node": "^10.15.2",
    "react-dom": "^16.8.4"
  },
  "devDependencies": {
    "@babel/core": "^7.0.0-bridge.0",
    "babel": "^5.8.23",
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "react": "^16.8.4",
    "webpack": "^4.29.6",
    "webpack-cli": "^3.2.3"
  }
}

These are my initial "devDependencies"

 "devDependencies": {
    "@babel/core": "^7.3.4",
    "babel": "^5.8.23",
    "babel-core": "^6.26.3",
    "babel-loader": "^8.0.5"
  }

My Node version is: v11.10.1

After trying the suggestions with:

rules:[
            {
                test: /\.(js|jsx)$/,
                exclude: '/node_modules/',
                use: [
                    { loader: 'babel-loader' }
                  ]
            }
        ]

I still get the same error.


Solution

  • This problem can be solved by setting each babel dependencies

    >=7.8.7

    "devDependencies": {
        "@babel/cli": "^7.13.10",
        "@babel/core": "^7.13.10",
        "@babel/preset-env": "^7.13.10",
        "@babel/preset-react": "^7.12.13",
    }