【发布时间】:2018-07-06 15:51:59
【问题描述】:
我正在尝试创建我的第一个 Electron 应用程序。我决定使用以下工具/技术:
- 打字稿
- WebPack(第 3 版)
- 反应
本地环境是 OS X High Sierra。
问题是我什至无法构建我的应用程序,并且在使用 WebPack 构建时出现错误:“Module not found: Error: Can't resolve 'fs' in '<root>/node_modules/electron'”
我的配置如下: 包.json:
"dependencies": {
"electron": "^1.7.11"
},
"devDependencies": {
"ts-loader": "^3.3.1",
"tslint": "^5.9.1",
"typescript": "^2.6.2",
"webpack": "^3.10.0"
}
tsconfig.json:
{
"compileOnSave": false,
"compilerOptions": {
"allowJs": true,
"jsx": "react",
"moduleResolution": "node",
"noImplicitAny": true,
"outDir": "./dist/",
"sourceMap": true,
"target": "es2015"
}
}
webpack.config.js:
const path = require('path');
module.exports = {
entry: './src/index.ts',
devtool: 'inline-source-map',
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
}
]
},
// node: {
// 'fs': 'empty'
// },
resolve: {
extensions: [ '.tsx', '.ts', '.js' ]
},
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
}
};
最后,我从电子教程中获取的唯一源代码文件 (./src/index.ts) 如下所示:
import { app, BrowserWindow } from 'electron';
import * as path from 'path';
import * as url from 'url';
let win: Electron.BrowserWindow;
function createWindow () {
// ... common stuff
}
app.on('ready', createWindow);
app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit(); }});
app.on('activate', () => { if (win === null) { createWindow(); }});
我认为问题在于 TypeScript 的使用方式,因为如果我将 index.ts 中的此类代码放入纯 js 文件中,它可以正常工作(将 'import' 替换为 'require')。
提前感谢您的帮助!
更新
如果在webpack.config.js 中设置{ target: 'node', },则构建步骤没有错误,但如果我尝试打开应用程序,我会得到:
App threw an error during load
Error: Electron failed to install correctly, please delete node_modules/electron and try installing again
重新安装节点模块没有帮助。
【问题讨论】:
标签: typescript webpack electron