3
votes

Update:

Code pushed to https://github.com/gsouvik/react_spa_experiment

Initial Post:

I know there are hundreds of threads out there, some had typos in webpack config, some used the loaders in a wrong way, some got it solved, some still open. But after numerous tries I still cannot get this working, a simple "Hello World" using Webpack 4, React js.

What I did

I was following this video tutorial line by line: React & Webpack 4 from scratch

My package.json

{
  "name": "my_react_experiment_2",
  "version": "1.0.0",
  "description": "Basic react with webpack ",
  "main": "index.js",
  "scripts": {
    "dev": "webpack-dev-server --mode development --hot",
    "build": "webpack --mode production"
  },
  "author": "Souvik Ghosh",
  "license": "ISC",
  "dependencies": {
    "react": "^16.4.2",
    "react-dom": "^16.4.2"
  },
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "babel-preset-env": "^1.7.0",
    "babel-preset-react": "^6.24.1",
    "html-webpack-plugin": "^3.2.0",
    "webpack": "^4.17.1",
    "webpack-cli": "^3.1.0",
    "webpack-dev-server": "^3.1.5"
  }
}

My webpack.config.js

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

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

My .babelrc

{
  "presets": ["env", "react"]
}

My directory structure

enter image description here

Expected behaviour

I fire up the dev server npm run dev. Expected to see my shiny new React js page saying "My first React Webpack project" (from the component /components/App.js)

Actual Behavior

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. | import App from "./components/App"; | ReactDOM.render(, document.getElementById('root')); | //ReactDOM.render('Hello User ', document.getElementById('root')); @ multi (webpack)-dev-server/client?http://localhost:8080 (webpack)/hot/dev-server.js ./src main2

If required I can share the codebase via a git repo. Any help is greatly appreciated.

1
Can you share the repo please ?Meet Zaveri
You can view my webpack config if you want to update. github.com/meetzaveri/react-doughnut/blob/master/…Meet Zaveri
Perhaps you need resolve: { modules: [ path.resolve("./src"), path.resolve("./node_modules") ], extensions: [".js"] }Hemadri Dasari
@MeetZaveri I've updated the original post with the git repo. Thanks.Souvik Ghosh

1 Answers

3
votes

The issue is with typo in your webpack config file.

You have module.export which is not correct. It should be module.exports

Working example

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

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