gpt4 book ai didi

node.js - 在 ionic + electron (5.0.0) 桌面应用程序中需要 Node 模块

转载 作者:搜寻专家 更新时间:2023-11-01 00:48:19 25 4
gpt4 key购买 nike

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

我开始使用 electron v4.1.3 并且我能够在应用程序的“ ionic 部分”中要求 Node 模块,例如在 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"));
}
}

这就是我得到的:

Screen of ionic app default home page

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

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

window.require is not a function

Screen of ionic app default home page with node module require error

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

最佳答案

根据重大更改文档,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 }
});

关于node.js - 在 ionic + electron (5.0.0) 桌面应用程序中需要 Node 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55891290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com