Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix Module parse failed: Unexpected token?

When I run npm run build I got this error:

ERROR in ./src/client.js 7:2
Module parse failed: Unexpected token (7:2)
File was processed with these loaders:
 * ./node_modules/jshint-loader/index.js
You may need an additional loader to handle the result of these loaders.
| 
| const component = (
>   <Router history={browserHistory}>
|     {routes}
|   </Router>
 @ multi babel-polyfill ./src/client.js main[1]

./src/client.js (with warning for browserHistory -- Cannot resolve symbol 'browserHistory' )

import React      from 'react';
import ReactDOM   from 'react-dom';
import { browserHistory, Router } from 'react-router';
import routes from './routes';

const component = (
  <Router history={browserHistory}>
    {routes}
  </Router>
);

ReactDOM.render(component, document.getElementById('react-view'));

How can I fix it?

react: 16.13.0

webpack: 4.42.0

npm: 6.14.2

webpack-config.js

global.Promise         = require('bluebird');
const webpack            = require('webpack');
const path               = require('path');

const plugins = [
  new webpack.DefinePlugin({
    'process.env': {
      BROWSER:  JSON.stringify(true),
      NODE_ENV: JSON.stringify(process.env.NODE_ENV || 'development')
    }
  })
];

module.exports = {
  entry: ['babel-polyfill', './src/client.js'],
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
  },

  module: {
    rules: [{
      test: /\.js$/, // include .js files
      enforce: "pre", // preload the jshint loader
      exclude: /node_modules/, // exclude any and all files in the node_modules folder
      use: [{
        loader: "jshint-loader",
        // more options in the optional jshint object
        options: {  // ⬅ formally jshint property
          camelcase: true,
          emitErrors: false,
          failOnHint: false
        }
      }]
    }]
  }
};
like image 217
Jack Avatar asked Aug 16 '26 06:08

Jack


1 Answers

Your webpack config doesn't handle React's JSX syntax. You need to update it with some loaders for this to work (here's a tutorial: https://www.valentinog.com/blog/babel/)

I would recommend you use create-react-app at first, which abstracts all those configurations away: https://github.com/facebook/create-react-app . It is way easier to get started with react this way and you can customise it when you're ready/if you need to.

like image 63
tudor.gergely Avatar answered Aug 17 '26 19:08

tudor.gergely



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!