Uncaught ReferenceError: require is not defined

What is the current behavior?
Thrown the error Uncaught ReferenceError: require is not defined

If the current behavior is a bug, please provide the steps to reproduce.
Just running webpack command with the below config

/**
 * Created by abdi.fakhruddin on 5/5/17.
 */

var ExtractTextPlugin = require('extract-text-webpack-plugin');
var path = require('path');
var autoprefixer = require('autoprefixer');
const webpack = require('webpack');
var fs = require('fs')

module.exports = {

  entry: './src/client.js',

  output: {
    path: path.join(__dirname, 'public'),
    filename: 'bundle.js'
  },

  target: 'node',

  // keep node_module paths out of the bundle
  externals: fs.readdirSync(path.resolve(__dirname, 'node_modules')).concat([
    'react-dom/server', 'react/addons',
  ]).reduce(function (ext, mod) {
    ext[mod] = 'commonjs ' + mod
    return ext
  }, {}),

  node: {
    __filename: true,
    __dirname: true
  },

  plugins: [
    new ExtractTextPlugin('styles.css'),
  ],

  module: {
    loaders: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: [
          /node_modules/,
          /\/src\/.*\/.*\/.*.test.js\//
        ],
      }, {
        test: /\.(png|jpg|jpeg|gif|svg|woff|woff2)$/,
        loader: 'url-loader?limit=100000&name=/[hash].[ext]',
      }, {
        test: /\.scss$/,
        loader: ExtractTextPlugin.extract(['css-loader', 'sass-loader']),
      },
      { test: /\.json$/, loader: "json-loader"}
    ],
  },

  devtool: 'cheap-module-source-map'

}

What is the expected behavior?
Build the project and run it with http-server

Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.
Node: 6.6.1
webpack: 2.5.0
OS: Mac OSX
Browser: chrome

**The error **

Uncaught ReferenceError: require is not defined
(anonymous)	@	external "babel-polyfill":1
__webpack_require__	@	bootstrap 59b71a2…:19
(anonymous)	@	client.js:5
__webpack_require__	@	bootstrap 59b71a2…:19
(anonymous)	@	bootstrap 59b71a2…:39
(anonymous)	@	bootstrap 59b71a2…:39

Author: Fantashit

2 thoughts on “Uncaught ReferenceError: require is not defined

Comments are closed.

Uncaught ReferenceError: require is not defined

Preflight Checklist

  • I have read the Contributing Guidelines for this project.
  • I agree to follow the Code of Conduct that this project adheres to.
  • I have searched the issue tracker for an issue that matches the one I want to file, without success.

Issue Details

  • Electron Version:
    • 5.0.0
  • Operating System:
    • Windows 10 (1903)
  • Last Known Working Electron version::
    • 4.1.5

Expected Behavior

In the lower version of 5.0.0, the app run properly means version 4.1.5 and below it works fine.
But in the version 5.0.0 it gets an error.
When I create a second js file and write 'require("electron")' it gets the error.

Actual Behavior

Uncaught ReferenceError: require is not defined

To Reproduce

GitHub

Screenshots

win1

Author: Fantashit

1 thought on “Uncaught ReferenceError: require is not defined

  1. In case the above suggestions didn’t work, you can try the following.

    In the main.js file (the main renderer process), where you’ve created the browser window, set the nodeIntegration to true as follows:

    winOne= new BrowserWindow({       
           /* other options like width,height*/
            webPreferences: {
                nodeIntegration: true
    }
        });

    Worked for me. Electron version: ^9.0.4

Comments are closed.

Uncaught ReferenceError: require is not defined

  • Electron Version:
    from 5.0.0-beta.5 to 5.0.0-beta.1
  • Operating System:
    macOS 10.14.3
  • Last Known Working Electron version::
    4.0.7

Problem

Build and pack passed well, but after run app, I see an error in console Uncaught ReferenceError: require is not defined - at Object.electron - at __webpack_require__.

Screenshots

screenshot 2019-03-06 at 15 20 05

Additional Information

We use Typescript, electron-builder, webpack.

Author: Fantashit

6 thoughts on “Uncaught ReferenceError: require is not defined

  1. The default for nodeIntegration changed from true to false as per our breaking changes document.

  2. in your index.js

    const w = new BrowserWindow({
    webPreferences: {
    nodeIntegration: true
    }
    })

    Thanks! Have solved my problem.

  3. Need to change webPreferences.nodeIntegration to true when new BrowserWindow
    mainWindow = new BrowserWindow({ width: 1000, height: 600, webPreferences: { nodeIntegration: true } });

Comments are closed.