MasterT MasterT - 1 year ago 202
React JSX Question

Bundle error using webpack for Electron application `Cannot resolve module 'electron'`

I am trying to create an Electron application with React. I use Webpack to compile the React JSX syntax, but when I try to compile with

command, I got this error:

ERROR in ./app.jsx
Module not found: Error: Cannot resolve module 'electron' in /Users/masterT/Downloads/gist

@ ./app.jsx 6:18-37

Here is the application code.

I am doing something wrong?


Answer Source

Webpack try to resolve electron module with installed node_modules. But the electron module is resolved in Electron runtime. So, you have to exclude particular module from webpack bundling like this:


module.exports = {
  entry: './app.jsx',
  output: {
    path: './built',
    filename: 'app.js'
  target: 'atom',
  module: {
    loaders: [
        loader: 'babel',
        test: /\.jsx$/,
        query: {
          presets: ['es2015', 'react']
  externals: [
    (function () {
      var IGNORES = [
      return function (context, request, callback) {
        if (IGNORES.indexOf(request) >= 0) {
          return callback(null, "require('" + request + "')");
        return callback();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download