0

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.

4 Answers 4

1

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",
}

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

Comments

0

ok I think your webpack test needs to include .js files if you plan to use JSX in them:

test: /\.(js|jsx)$/

1 Comment

I tried both test: /\.(js|jsx)$/ and test: /\.(js|jsx)$/ Did not solve the problem.
0

After a full day of hacking and surfing this site I found a solution.

module:{
        rules:[
            {
                test: /\.jsx?$/,
                exclude: '/node_modules/',
                loader: 'babel-loader',
                query:
                {
                    presets:['react']
                }  
            }
        ]
    }

And the package.json partial:

  "devDependencies": {
    "@babel/core": "^7.3.4",
    "babel": "^5.8.23",
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "babel-preset-react": "^6.24.1",
    "babel-preset-react-app": "^7.0.2",
    "react": "^16.8.4",
    "webpack": "^4.29.6",
    "webpack-cli": "^3.2.3"
  }

Comments

0

with that config you are telling to only "translate" .jsx files, and since you main file is index.js, he is not loading it, try adding this config:

with this you add js and jsx file to be resolved by your loader

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

my fault i thought you had a babelrc, i forgot you can create a file on same folder level as your wepback.config with ".babelrc" name and here you can specify your loading config, and tell babel to load "react" plugin

{
"presets": [
  "stage-0",
  "react",
  "es2015"
],
 "plugins": [
  "transform-class-properties",
  "transform-decorators-legacy",
  "transform-object-rest-spread",
  "transform-es2015-destructuring"
 ],
 "env": {
   "debug": true
 }
}

1 Comment

I tried this and it did not solve the problem, same error.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.