问题描述
我正在使用 electron-builder (16.6.2) 打包我的电子应用程序,其中包括 keytar (3.0.2) 作为产品依赖项.
I'm using electron-builder (16.6.2) to package my electron application which includes keytar (3.0.2) as a prod dependency.
package.json 文件包括:
package.json file includes:
"scripts": {
"postinstall": "install-app-deps",
"compile:dev": "webpack-dev-server --hot --host 0.0.0.0 --config=./webpack.dev.config.js",
"compile": "webpack --config webpack.build.config.js",
"dist": "yarn compile && build"
},
"build": {
"appId": "com.myproject",
"asar": true,
"files": [
"bin",
"node_modules",
"main.js"
]
}
当我在同一系统上运行 .app 时,它运行良好.当我尝试在不同的系统上运行它(或删除我的 node_modules)时,它找不到 keytar.node.构建 keytar 时,它包含我系统的该映像的完全限定路径.我在控制台中收到以下错误:
When I run the .app on the same system it runs fine. When I try running it on a different system (or deleting my node_modules) it fails to find keytar.node. When keytar is built, it includes a fully qualified path to that image for my system. I get the following error in the console:
Uncaught Error: Cannot open /Users/Kevin/Work/myproject/node_modules/keytar/build/Release/keytar.node
Error: dlopen(/Users/Kevin/Work/myproject/node_modules/keytar/build/Release/keytar.node,
1): image not found
我一定是在构建过程中遗漏了一个步骤.
I must be missing a step in the build process.
推荐答案
事实证明,我在渲染器进程中使用了 keytar.我将 keytar 移到主进程中(不通过 Webpack/Babel),并被电子生成器正确打包.
As it turns out, I was using keytar in the renderer process. I moved keytar into the main process (which doesn't go through Webpack / Babel) and gets packed correctly by electron-builder.
main.js
ipcMain.on('get-password', (event, user) => {
event.returnValue = keytar.getPassword('ServiceName', user);
});
ipcMain.on('set-password', (event, user, pass) => {
event.returnValue = keytar.replacePassword('ServiceName', user, pass);
});
然后我可以从渲染器进程调用
Then from the renderer process I can call
const password = ipcRenderer.sendSync('get-password', user);
或
ipcRenderer.sendSync('set-password', user, pass);
这篇关于使用 Electron 应用程序打包 Keytar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!