AntonHPL
AntonHPL

Reputation: 123

Webpack React error: Module parse failed: Unexpected token

Webpack is installed to React. But when I try to create a dist file there is an error. How to fix that? enter image description here

From package.json file:

  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "builder": "webpack"
  },

Webpack.config.js:

const path = require('path');

module.exports = {
  mode: 'development',
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js',
  },
};

index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(<App />,
  document.getElementById('root')
);

Upvotes: 4

Views: 12221

Answers (1)

G&#252;lsen Keskin
G&#252;lsen Keskin

Reputation: 810

You may need an appropirate loader to handle this file type. try this:

const path = require('path');

module.exports = {
  mode: 'development',
  entry: './src/index.js',
  module: {
    rules: [
      {
        test: /\.(js)$/,
        exclude: /node_modules/,
        use: ['babel-loader']
      }
    ]
  },
  resolve: {
    extensions: ['*', '.js']
  },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js',
  },
};


npm i @babel/core @babel/preset-env babel-loader --save-dev

Upvotes: 2

Related Questions