无法在 electron-forge 应用程序中使用本机节点模块

cannot use native node module in electron-forge app

我正在尝试在使用 electron-forge 使用 webpack 模板创建的 electron 应用程序中使用 pkcs11js

但是我得到了错误

Error: C:\ws-p\electron-test\.webpack\main9a63bd9833f80e7879a5bbfb2a4af2.node is not a valid Win32 application.
←[90m    at Object.Module._extensions..node (internal/modules/cjs/loader.js:1122:18)←[39m
←[90m    at Module.load (internal/modules/cjs/loader.js:928:32)←[39m
←[90m    at Function.Module._load (internal/modules/cjs/loader.js:769:14)←[39m
←[90m    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)←[39m
←[90m    at internal/main/run_main_module.js:17:47←[39m

周围有很多问题,但到目前为止 solutions/answers 所提供的 none 对我有用。

None 个结果:

环境:

Windows 10 Pro
Node 14.16.1
electron: 13.1.0
electron-rebuild: 2.3.5
electron-forge: 6.0.0-beta.57
pkcs11js: 1.2.4

您应该使用 Electron Forge Webpack template,它对本机模块有更好的支持。

目前有一个open issue for this functionality caused by the outdated/unmaintained @marshallofsound/webpack-asset-relocator-loader which caters for native modules via Webpack. I'm currently working on a PR to fix this but in the meantime you can use my updated fork

None 网络上的解决方案对我有用。有效的方法是在 electron-rebuild 之前将 electron-gyp 输出文件夹定义为主文件夹,或者使用 electron-forge 正确的 after/before 依赖项安装。

在终端中:

Windows:

set HOME=C:\Users\YourUser\.electron-gyp
npm i
npm run start <# which resolves to electron-forge start #> 

Unix:

$ export HOME=~/.electron-gyp
$ npm i
$ npm run start # which resolves to  electron-forge start

希望对大家有所帮助