简体   繁体   中英

cannot use native node module in electron-forge app

I am trying to use pkcs11js in an electron app created with electron-forge using webpack template.

But I got the error

Error: C:\ws-p\electron-test\.webpack\main\349a63bd9833f80e7879a5bbfb2a4af2.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

There are lots of questions asked around but none of the provided solutions/answers worked for me so far.

None of these worked out:

  • remove & reinstall node_modules
  • use electron-rebuild (although electron-forge uses this internally)
  • use node-gyp rebuild --target=13.1.0 --arch=x64 --dist-url=https://electronjs.org/headers manually for the package
  • Npm steps defined in electron docs

Environment :

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

You should use the Electron Forge Webpack template which has better support for native modules.

There is currently anopen 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 of the solutions on the web worked for me. What worked is defining electron-gyp output folder as home before electron-rebuild or with electron-forge right after/before dependencies install.

In a terminal:

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

I hope it helps to someone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM