1

I'm getting this error on Chrome developer tools: "Uncaught Reference: Require is not defined"

webpack.config

var path = require('path');
var webpack = require('webpack');

module.exports = {
    entry: './src/app.js',
    output: {
        filename: 'app.js',
        path: __dirname + "public/scripts"
    },
    module: {
        loaders: [
            {
                test: /.jsx?$/,
                loader: "babel-loader",
                exclude: /node_modules/,
                query: {
                    presets: ["env", "react"]
                }
            }
        ]
    }
};

package.json

{
  "name": "widget",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Diogo Matias",
  "license": "ISC",
  "dependencies": {
    "babel-cli": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react": "^6.24.1",
    "npm": "^5.6.0",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-google-maps": "^9.4.3",
    "react-leaflet": "^1.7.8"
  },
  "devDependencies": {
    "webpack": "^3.10.0"
  }
}

This error comes from the import on my source javascript file where I have:

 import {GoogleMap, Marker} from "react-google-maps";

I use the cdn for react and react-dom. I'm used to use live-server and I'm new to webpack. I run with webpack-dev-server on the root directory. Hope that is enough for you to help me.

Thanks.

2
  • You only have babel-loader for jsx files but your entry point is a js file. test: /\.js$/, should fix it. Commented Dec 29, 2017 at 17:50
  • Make sure your babel presets looks like this { "presets": [ "es2015", "react" ], "plugins": [ "transform-object-rest-spread" ] } Commented Dec 29, 2017 at 17:52

1 Answer 1

1

Here is a minimum configuration to get you started. Issue npm run start to bring up webpack-dev-server at port 9000. babel-core was missing from your configuration.

project structure

├── build
│   └── index.html
├── package.json
├── src
│   └── app.js
└── webpack.config.js

package.json

{
  "name": "widget",
  "version": "1.0.0",
  "description": "Get started with Webpack!",
  "scripts": {
    "build": "webpack",
    "start": "webpack-dev-server"
  },
  "author": "Diogo Matias",
  "license": "ISC",
  "dependencies": {
    "leaflet": "^1.2.0",
    "prop-types": "^15.5.0",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-google-maps": "^9.4.3",
    "react-leaflet": "^1.7.8"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react": "^6.24.1",
    "webpack": "^3.10.0",
    "webpack-dev-server": "^2.9.7"
  }
}

webpack.config.js

var path = require('path');
var webpack = require('webpack');

module.exports = {
  entry: './src/app.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'build')
  },
  devServer: {
    contentBase: path.join(__dirname, 'build'),
    compress: true,
    port: 9000
  },
  module: {
    loaders: [
      {
        test: /.jsx?$/,
        loader: "babel-loader",
        exclude: /node_modules/
      }
    ]
  }
};

.babelrc

{
  "presets": [
    ["env", {
      "targets": {
        "browsers": ["last 3 Chrome versions"]
      }
    }]
  ]
}

build/index.html

<html lang="en">
  <head>
    <meta charset="utf-8">
  </head>

  <body>
    <div id="root"></div>
    <script src="bundle.js"></script>
  </body>
</html>
Sign up to request clarification or add additional context in comments.

2 Comments

It is only missing the presets on the webpack.config.js. Don't mean to sound ungrateful. Thanks very much, totally made my day.
Although this, I do enjoy to ask for the command to run webpack automatically whenever there is a change in the files?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.