node.jselectronreact-boilerplatenode-ffi

Require ('ffi') throws error in Electron application


I am working on an Electron.js application and need to use the node-ffi module to use a С++ dll library. I have installed the module and tried to add require('ffi') into my app\electron\main.dev.js, but got an error:

UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The
"path" argument must be of type string. Received type object... 

The require('ffi') works as expected, for example, in this simple project

After some invesigation I have found, that Error is triggered in ...node_modules\bindings\bindings.js:

exports.getRoot = function getRoot (file) {
  var dir = dirname(file) // here!

file is undefined here...

My project is here.

Full Error Message:

UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The >"path" argument must be of type string. Received type object
   at assertPath (path.js:39:11)
   at dirname (path.js:651:5)
   at Function.getRoot 
(D:\JavaScript\ElectronReactBoilerplate4\app\electron\main.prod.js:482:13)
   at bindings 
(D:\JavaScript\ElectronReactBoilerplate4\app\electron\main.prod.js:391:32)
   at Object../node_modules/ref/lib/ref.js 
(D:\JavaScript\ElectronReactBoilerplate4\app\electron\main.prod.js:7148:102)
   at _webpack_require__ 
(D:\JavaScript\ElectronReactBoilerplate4\app\electron\main.prod.js:21:30)
   at Object../node_modules/ffi/lib/ffi.js 
(D:\JavaScript\ElectronReactBoilerplate4\app\electron\main.prod.js:2619:11)
   at _webpack_require__ 
(D:\JavaScript\ElectronReactBoilerplate4\app\electron\main.prod.js:21:30)
   at App.<anonymous> 
(D:\JavaScript\ElectronReactBoilerplate4\app\electron\main.prod.js:196:3)
   at Generator.next (<anonymous>)...

What a magic is going here?

How to get the require('ffi') working in my project?

Edited:
... replaced false in internals\webpack\webpack.main.dev.js with true

node: {
    __dirname: true,
    __filename: true,
  },

and now Error message is

(node:16996) UnhandledPromiseRejectionWarning: Error: Could not locate the bindings file. Tried:
 тЖТ D:\AndrewReactElectron\ElectronReactBoilerplate4\app\build\binding.node
 тЖТ D:\AndrewReactElectron\ElectronReactBoilerplate4\app\build\Debug\binding.node
 тЖТ D:\AndrewReactElectron\ElectronReactBoilerplate4\app\build\Release\binding.node
 тЖТ D:\AndrewReactElectron\ElectronReactBoilerplate4\app\out\Debug\binding.node
 тЖТ D:\AndrewReactElectron\ElectronReactBoilerplate4\app\Debug\binding.node
 тЖТ D:\AndrewReactElectron\ElectronReactBoilerplate4\app\out\Release\binding.node
 тЖТ D:\AndrewReactElectron\ElectronReactBoilerplate4\app\Release\binding.node
 тЖТ D:\AndrewReactElectron\ElectronReactBoilerplate4\app\build\default\binding.node
 тЖТ D:\AndrewReactElectron\ElectronReactBoilerplate4\app\compiled\10.11.0\win32\ia32\binding.node
 тЖТ D:\AndrewReactElectron\ElectronReactBoilerplate4\app\addon-build\release\install-root\binding.node
 тЖТ D:\AndrewReactElectron\ElectronReactBoilerplate4\app\addon-build\debug\install-root\binding.node
 тЖТ D:\AndrewReactElectron\ElectronReactBoilerplate4\app\addon-build\default\install-root\binding.node
 тЖТ D:\AndrewReactElectron\ElectronReactBoilerplate4\app\lib\binding\node-v69-win32-ia32\binding.node
    at bindings (D:\AndrewReactElectron\ElectronReactBoilerplate4\app\electron\main.prod.js:445:9)

Any ideas?


Solution

  • Adding externals: { ffi: 'ffi' } into my webpack.main.prod.js has solved the problem