使用电子应用程序的webpack捆绑错误`无法parsing模块'electron'`

我正尝试用React创build一个Electron应用程序。 我使用Webpack编译React JSX语法,但是当我尝试使用webpack命令编译时,出现这个错误:

./app.jsx中的错误找不到模块:错误:无法parsing/ Users / masterT / Downloads / gist中的模块'electron'

@ ./app.jsx 6:18-37

这是应用程序代码 。

我做错了什么?

谢谢!

Webpack尝试使用已安装的node_modules来parsingelectron模块。 但electron模块在运行时在电子本身parsing。 所以,你必须像这样从webpack捆绑中排除特定的模块:

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(); }; })() ] }; 

非常简单的解决scheme:

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

webpack会忽略这个要求

你可以在你的webpackconfiguration中设置target: 'electron' ,然后你不必在外部排除电子。

从webpack文档 :

"electron"编译用于电子 – 支持require – 电子专用模块。

你的package.json有'电子预编译',但是你的代码需要'电子'。 你有没有试过要求“电子预制”?