使用webpack的电子应用程序出现捆绑错误,无法解析模块“电子” [英] Bundle error using webpack for Electron application `Cannot resolve module 'electron'`
问题描述
我正在尝试使用 React 创建一个 Electron 应用程序。我使用 Webpack 来编译 React JSX 语法,但是当我尝试使用 webpack
命令进行编译时,我得到了错误:
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:
./ app.jsx
中的错误找不到模块:错误:无法解析/ Users / masterT / Downloads中的模块'electron' / gist
ERROR in ./app.jsx Module not found: Error: Cannot resolve module 'electron' in /Users/masterT/Downloads/gist
@ ./app.jsx 6:18-37
@ ./app.jsx 6:18-37
这是应用程序代码。
我做错了什么?
谢谢!
推荐答案
Webpack尝试解析电子
带有已安装的node_modules的模块。但是 electron
模块在运行时由Electron本身解决。因此,您必须像这样从webpack捆绑中排除特定的模块:
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:
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();
};
})()
]
};
这篇关于使用webpack的电子应用程序出现捆绑错误,无法解析模块“电子”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!