Setting node.__dirname to false doesn't work with ffi-napi, ref-napi libraries with electron js
See original GitHub issueBug report
What is the current behavior?
Using webpack in the electron js app I can’t import native module libraries like ffi-napi
, ref-napi
, etc in my application. Libraries like those generally expects node’s __dirname
to set as global. Thus, setting node: {__dirname: true}
would work in my app but in the production
built it throws a runtime error:
Error: No native build was found for platform=darwin arch=x64 runtime=electron abi=87 uv=1 libc=glibc node=14.16.0 electron=12.0.6 webpack=true
The same above error would throws if I change/remove node:{__dirname: false}
or node
property itself from the webpack config.
If the current behavior is a bug, please provide the steps to reproduce. I’m not sure on this whether its a bug or not. But to reproduce the scenario here’s my example repo which can show the issue in more details. Basically,
to reproduce an error
- start the app by
npm start
- then an error would shown up
to get working
- change the
__dirname
totrue
in thewebpack.config.js
and again - start the app by
npm start
- now it runs OK. To confirm if native module
ffi-napi
,ref-napi
are working properly - goto View > Toggle Developer Tools and in the console tab =Add: 8
What is the expected behavior?
If I need to set node:{__dirname: true}
in this case (which works in the development
mode) then the same behavior is expected to be working in the production
mode (in the installer). Otherwise, if there is/are other work around to fix then still the expectation is same as to the development
mode at the moment.
Other relevant information: webpack version: ^5.37.0 Node.js version: 14.16.0 Operating System: macOS 10.14.6 Additional tools: ffi-napi ver. ^4.0.3, ref-napi ver. ^3.0.2, webpack-cli ver. ^4.7.0, electron ver. ^12.0.7
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (5 by maintainers)
Top GitHub Comments
@alexander-akait Yeah its to do with node addon path that node gyp build is expecting for building. But the
__dirname
varies based on the webpack config and so does the electron as well. One definitely needed to settle by providing consistent path to the directory and I fixed by using electron’sgetAppPath
function which always returns runtime app execution path. Node loader may not work in this case I think as libraries likeffi-napi
,ref-napi
themselves provide javascript apis to interact with their node addon files. However, did have to customized a bit of those libraries for me to get working. Thanks for the heads up though. I think this issue can be closed now.hm, yes, you are right
target: 'node'
is not working and it is expected, we set__dirname: mock
for all cases exceptnode
https://github.com/webpack/webpack/blob/master/lib/config/defaults.js#L884The problem here is that
node-gyp-build
try to search file and don’t found it, because it is bundled code and be indist
directory, not innode_modules
(in my case it is/home/akait/projects/electron-webpack-native-module-example/dist
), so node-gyp-build trying to search on the wrong path. Opennode_modules/node-gyp-build/index.js
and debugload.path
.We have the same limitation for Node.js add-ons, you need to use https://github.com/webpack-contrib/node-loader to working with them.
I think duplicate https://github.com/webpack/webpack/issues/5424