question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setting node.__dirname to false doesn't work with ffi-napi, ref-napi libraries with electron js

See original GitHub issue

Bug 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 to true in the webpack.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:closed
  • Created 2 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
raviSussolcommented, May 22, 2021

@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’s getAppPath function which always returns runtime app execution path. Node loader may not work in this case I think as libraries like ffi-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.

0reactions
alexander-akaitcommented, May 15, 2021

hm, yes, you are right target: 'node' is not working and it is expected, we set __dirname: mock for all cases except node https://github.com/webpack/webpack/blob/master/lib/config/defaults.js#L884

The problem here is that node-gyp-build try to search file and don’t found it, because it is bundled code and be in dist directory, not in node_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. Open node_modules/node-gyp-build/index.js and debug load.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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Electron App Not Working With `ffi-napi` Module - Stack Overflow
My main objective is to access windows.h functions inside an electron application. ffi-node seems to be working fine as a separate node ......
Read more >
node-ffi-napi - Bountysource
I have an electron js application that uses ffi-napi as a native module. ... __dirname to true i.e. in the webpack config file:...
Read more >
Implementing A Svelte Store In Rust - Dave Ceddia
We're going to build a Svelte store, written in Rust, and run it in an Electron app.
Read more >
Windows Analysis Report Design Extractor.exe - Joe Sandbox
true, false ... shared libraries that want to be imported as extensions into. ... "url": "git://github.com/node-ffi-napi/ref-napi.git". },.
Read more >
Viewing online file analysis results for 'app.asar'
Submit malware for free analysis with Falcon Sandbox and Hybrid Analysis technology. Hybrid Analysis develops and licenses analysis tools to fight malware.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found