0
votes

New to webpack with react. Getting this error when building webpack.

ERROR in ./dist/index.js 6:16 Module parse failed: Unexpected token (6:16) You may need an appropriate loader to handle this file type. | |

ReactDOM.render(<Fetch />, document.getElementById("root"))

dist/index.js

import React, { Component } from "react";
import ReactDOM from "react-dom";
import Fetch from "../src/index"


ReactDOM.render(<Fetch />, document.getElementById("root"))

webpack.config.js

var path = require('path');
module.exports = {
  entry: './dist/index.js',
  output: {
    path: path.resolve(__dirname, './'),
    filename: 'index.js',
    libraryTarget: 'commonjs2'
  },

  resolve: {
    extensions: [  '.js', '.jsx']
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        include: path.resolve(__dirname, './src'),
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: { 
            presets: ['@babel/preset-env', '@babel/react'],
            plugins:['@babel/plugin-proposal-class-properties']
          }
        }
      }
    ]
  },
  devServer: {
    contentBase: path.join(__dirname, "dist"),
    compress: true,
    port: 9000,
    watchContentBase: true,
    progress: true
  },
  externals: {
    'react': 'commonjs react',
    'reactDOM': 'react-dom'
  },
};

src/index.js

import Fetch from './Fetch';

export default Fetch;
1

1 Answers

1
votes

I was able to get Webpack to build using your webpack.config.js by removing the line:

include: path.resolve(__dirname, './src')

which is located within the rules for babel-loader. I also changed the output's path to:

path: __dirname