我在使Keytar包在 Electron Forge v6 项目(beta.54)中工作时遇到了一些麻烦。
在新生成的项目中,我在运行应用程序时收到有关“文件太短”的错误。下面是来自主进程的错误对话框的摘录:
> electron-forge start
✔ Checking your system
✔ Locating Application
✔ Preparing native dependencies: 1 / 1
✔ Compiling Main Process Code
✔ Launch Dev Servers
✔ Compiling Preload Scripts
✔ Launching Application
Webpack Output Available: http://localhost:9000
App threw an error during load
Error: node-loader:
Error: dlopen(/Users/abc/Desktop/app2/.webpack/main/9e6f9bf6669542cb15efba32c033f3de.node, 1): no suitable image found. Did find:
/Users/abc/Desktop/app2/.webpack/main/9e6f9bf6669542cb15efba32c033f3de.node: file too short
/Users/abc/Desktop/app2/.webpack/main/9e6f9bf6669542cb15efba32c033f3de.node: file too short
at Object.<anonymous> (/Users/abc/Desktop/app2/.webpack/main/index.js:1015:9)
at Object../node_modules/keytar/build/Release/keytar.node (/Users/abc/Desktop/app2/.webpack/main/index.js:1018:30)
at __webpack_require__ (/Users/abc/Desktop/app2/.webpack/main/index.js:21:30)
at Object../node_modules/keytar/lib/keytar.js (/Users/abc/Desktop/app2/.webpack/main/index.js:1029:14)
at __webpack_require__ (/Users/abc/Desktop/app2/.webpack/main/index.js:21:30)
at Object../src/index.ts (/Users/abc/Desktop/app2/.webpack/main/index.js:1440:14)
at __webpack_require__ (/Users/abc/Desktop/app2/.webpack/main/index.js:21:30)
at /Users/abc/Desktop/app2/.webpack/main/index.js:85:18
at Object.<anonymous> (/Users/abc/Desktop/app2/.webpack/main/index.js:88:10)
at Module._compile (internal/modules/cjs/loader.js:1152:30)
这个包似乎node-loader
有问题。我无法破译正在发生的事情,因为我可以看到包.node
文件被复制到./webpack/main/native_modules
中,但是在根目录中有一个散列文件名,它似乎导入了本机文件。
我注意到的一件奇怪的事情是,哈希文件.webpack/main/9e6f9bf6669542cb15efba32c033f3de.node
包含以下内容,而不是本机二进制数据:
module.exports = __non_webpack_require__("./native_modules/build/Release/keytar.node")
随后......这些.node
文件/native_modules/build/Release/keytar.node
是我所期望的。
应该如何进行?在弄清楚问题出在哪里之前,我并没有急于在 GitHub 上打开问题。
我觉得从根本上说,.node
文件的生成方式存在问题keytar
,但是谁知道 Webpack 和加载程序发生了什么黑魔法。
重现步骤
- 创建一个新的 Electron Forge 项目 `npx create-electron-app app2 --template=typescript-webpack
- 安装 Keytar
npm install keytar
- 添加
const key tar = require('keytar');
到src/index.ts
- 通过运行应用程序
npm run start
- 体验错误
版本
节点:v14.14.0
NPM:v7.0.13
打包 JSON 依赖项:
"devDependencies": {
"@electron-forge/cli": "^6.0.0-beta.54",
"@electron-forge/maker-deb": "^6.0.0-beta.54",
"@electron-forge/maker-rpm": "^6.0.0-beta.54",
"@electron-forge/maker-squirrel": "^6.0.0-beta.54",
"@electron-forge/maker-zip": "^6.0.0-beta.54",
"@electron-forge/plugin-webpack": "6.0.0-beta.54",
"@marshallofsound/webpack-asset-relocator-loader": "^0.5.0",
"@typescript-eslint/eslint-plugin": "^4.0.1",
"@typescript-eslint/parser": "^4.0.1",
"css-loader": "^4.2.1",
"electron": "11.0.3",
"eslint": "^7.6.0",
"eslint-plugin-import": "^2.20.0",
"fork-ts-checker-webpack-plugin": "^5.0.14",
"node-loader": "^1.0.1",
"style-loader": "^1.2.1",
"ts-loader": "^8.0.2",
"typescript": "^4.0.2"
},
"dependencies": {
"electron-squirrel-startup": "^1.0.0",
"keytar": "^7.2.0"
}