Bundle error using webpack for Electron applicatio

2019-01-22 07:43发布

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 webpack 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?

Thanks!

4条回答
Melony?
2楼-- · 2019-01-22 08:02

Your package.json has 'electron-prebuilt' but you require 'electron' in your code. Have you tried require-ing 'electron-prebuild'?

查看更多
Animai°情兽
3楼-- · 2019-01-22 08:06

A very simple solution :

const remote = window.require('electron').remote;

webpack will ignore this require

查看更多
神经病院院长
4楼-- · 2019-01-22 08:11

You can set target: 'electron' in your webpack config and then you don't have to exclude electron in externals.

From webpack documentation:

"electron" Compile for usage in Electron – supports require-ing Electron-specific modules.

查看更多
冷血范
5楼-- · 2019-01-22 08:19

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

webpack.config.js:

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 = [
        'electron'
      ];
      return function (context, request, callback) {
        if (IGNORES.indexOf(request) >= 0) {
          return callback(null, "require('" + request + "')");
        }
        return callback();
      };
    })()
  ]
};
查看更多
登录 后发表回答