Search code examples
reactjswebpackbabeljsbabel-loader

babel-loader not working, Unexpected token '<'


I've been banging my head to setup React app without create-react-app. But the only issue I'm facing all the time is this-

ERROR in ./src/index.js 5:16
Module parse failed: Unexpected token (5:16)
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
| import App from './comps/App';
|
> ReactDOM.render(<App/>, document.querySelector('#root'));

None of the solutions I found in my long-search fixed this issue.

Seems like the babel-loader doesn't work for me, nor does any other loader such as sass-loader. Initially the dev-dependencies I used were-

babel-loader babel-core babel-preset-env babel-preset-react

Later I replaced these with-

babel-loader @babel/core @babel/preset-env @babel/preset-react

but with the same result. Please help me resolve the issue!

package.json-

{
  "name": "react-webpack-starter",
  "version": "1.0.0",
  "description": "Boilerplate for React apps",
  "main": "index.js",
  "scripts": {
    "start": "webpack-dev-server --mode development --open --hot",
    "build": "webpack --mode production"
  },
  "author": "Sapinder",
  "license": "ISC",
  "dependencies": {
    "react": "^16.13.1",
    "react-dom": "^16.13.1"
  },
  "devDependencies": {
    "@babel/core": "^7.10.5",
    "@babel/preset-env": "^7.10.4",
    "@babel/preset-react": "^7.10.4",
    "babel-loader": "^8.1.0",
    "html-webpack-plugin": "^4.3.0",
    "webpack": "^4.44.0",
    "webpack-cli": "^3.3.12",
    "webpack-dev-server": "^3.11.0"
  }
}

.babelrc-

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

webpack.config.js-

const path = require('path');
const HTMLWebpackPlugin = require('html-webpack-plugin');

module.export = {
    entry: './src/index.js',
    output: {
        path: path.join(__dirname, '/dist'),
        filename: 'index_bundle.js'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader'
                }
            }
        ]
    },
    plugins: [
        new HTMLWebpackPlugin({
            template: './src/index.html'
        })
    ]
}

Solution

  • Let's try

    // webpack.config.js
    
    ...
    
    module.exports = {...};
    
    ...
    
    

    Notice the missing "s" in module.export in your example :D