在 ionic + electron (5.0.0) 桌面应用程序中需要节点模块

Requiring node modules in ionic + electron (5.0.0) desktop application

我正在使用 ionic 和 electron 构建桌面应用程序。

我开始使用 electron v4.1.3 并且我能够在应用程序的 "ionic part" 中要求节点模块,例如在 home.ts 文件中使用:

import { Component } from '@angular/core';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {

    ngOnInit () {
        console.log ((<any> window).require ("fs"));
    }
}

这就是我得到的:

如您所见,我可以访问所有 fs 方法,因此我可以读取、写入、复制文件以及其他任何内容。

现在我已经安装了 electron v5.0.0,我创建了相同的应用程序,但是当我尝试在 ngOnInit 方法中要求 fs 模块时出现错误:

window.require is not a function

我该如何解决这个问题?如果您需要有关安装或环境的更多详细信息,请告诉我,谢谢!

根据重大更改文档,nodeIntegration 现在在 5.0.0 中默认禁用。

https://github.com/electron/electron/blob/master/docs/api/breaking-changes.md#planned-breaking-api-changes-50

和发行说明

https://github.com/electron/electron/releases/tag/v5.0.0

所以你需要启用它:

const mainWindow = new BrowserWindow({
    webPreferences: { nodeIntegration: true }
  });