Electron 上的 Firebase 应用出现错误:无法加载 gRPC



我正在构建一个Electron应用程序,在渲染器.js文件中,我正在使用Firebase Admin来获取Firestore数据。但是,每当我运行它时,它都会在日志中返回此错误。

Error: Failed to load gRPC binary module because it was not installed for the current system
Expected directory: electron-v2.0-darwin-x64-unknown
Found: [node-v48-darwin-x64-unknown]
This problem can often be fixed by running "npm rebuild" on the current system

我尝试运行"npm 重建",但它仍然没有修复它。 我还尝试更新Firebase Admin和gRPC。

这是渲染器.js文件中的代码...

// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.
const admin = require('firebase-admin');
var serviceAccount = require('./credentials.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://mytestapp.firebaseio.com"
});
var db = admin.firestore();
const settings = {
timestampsInSnapshots: true
};
db.settings(settings);
function LoadList() {
db.collection("Orders").get().then(function(Collection){
Collection.forEach(function(OrderDoc){
console.log(OrderDoc.id)
})
}).catch(function(err){
console.error(err);
});
}
document.querySelector('#ListSec').addEventListener('click', LoadOrderList)

有什么想法吗?几个小时以来,我一直在尝试解决这个问题,但似乎无法弄清楚。

该错误消息指示 gRPC 是为 Node 安装的,而不是为 Electron 安装的。Electron具有不同的二进制接口,因此需要专门为Electron安装像gRPC这样的二进制模块。通常,您只需运行npm rebuild --runtime=electron --target=2.0.0即可执行此操作(修改以匹配要使用的Electron版本(。

@murgatroid99的原始答案在当时很有帮助,安装后命令在电子 v7 之前一直工作得很好,问题再次出现。

对于遇到此问题的其他任何人,我找到了更好的解决方案:

电子重建包

npm install electron-rebuild --save-dev

使用 运行它

npx electron-rebuild

或者,将其添加为安装后命令

{
...
"scripts": {
"postinstall": "electron-rebuild"
},
...
}

更多信息在官方电子文档中

最新更新