Electron-builder: Can not load native module in production

Created on 2 May 2018  路  8Comments  路  Source: electron-userland/electron-builder


  • 19.56.2:

  • ia32:


Hi,

I want to use a custom native addon module in my electron app. The module is located in

C:\my-application-root-dir\node_modules\myaddon\build\Release\myaddon.node

and in srcmain\index.js I require my module with relative path:

var myaddon = require('../../node_modules/myaddon/build/Release/myaddon')

in the package.json, I put my module in dependencies:

  "dependencies": {
    "myaddon":"^1.0.0",
  },

this config is working in the develop environment.

When I build my app with electron-builder for production, build was successfully and a setup file is generated, and I find the myaddon in unpacked directory under build

C:\my-application-root-dir\build\win-ia32-unpacked\resources\app.asar.unpacked\node_modules\myaddon\build\Release\myaddon.node

However after I install the application in another PC, A Javascript error occurred in the main process:

Cannot open C:\my-application-root-dir\node_modules\myaddon\build\Release\myaddon.node: Error: Uncaught Exception:
Error: Cannot find module .

After I search for answers for hours I found something in the document:

If you have native addons of your own that are part of the application (not as a dependency), set nodeGypRebuild to true.

does this mean, in my case, myaddon is not as a dependency? Should I put my myaddon inside /node_modules or in /src ?

And if I set nodeGypRebuild to true, the build process will fail, did I make something wrong?

Or the way I require my custom addon is wrong?

Any help would be appreciated.

Most helpful comment

It seems that node-loader use a hardcore absolute path:
https://github.com/webpack-contrib/node-loader/issues/12

issue solve by replace node-loader with native-ext-loader, I also test node-addon-loader, but it's not working.

webpack.main.config.js:

      {
        test: /\.node$/,
        loader: 'native-ext-loader',        
        //use: 'node-addon-loader',
        //use: 'node-loader',
        options: {
          //rewritePath: path.resolve(__dirname, 'dist')
        },           
      }

All 8 comments

I tried with that:

electron-userland/electron-packager#217

if (process.env.NODE_ENV === 'development') {
  var myaddon = require('../../node_modules/myaddon/build/Release/myaddon')  
} else {
  const path = require('path')
  const module = require('module')
  module.paths.push(path.resolve('node_modules'));
  module.paths.push(path.resolve('../node_modules'));
  module.paths.push(path.resolve(__dirname, '..', '..', '..', '..', 'resources', 'app', 'node_modules'));
  module.paths.push(path.resolve(__dirname, '..', '..', '..', '..', 'resources', 'app.asar', 'node_modules'));
  module.paths.push(path.resolve(__dirname, '..', '..', '..', '..', 'resources', 'app.asar.unpacked', 'node_modules'));
  var myaddon = require('myaddon/build/Release/myaddon')
}

but still not working for production

got same error

Uncaught Exception:
Error:
Cannot open C:\my-application-root-dir\node_modules\myaddon\build\Release\myaddon.node: Error: Cannot find module

It seems that node-loader use a hardcore absolute path:
https://github.com/webpack-contrib/node-loader/issues/12

issue solve by replace node-loader with native-ext-loader, I also test node-addon-loader, but it's not working.

webpack.main.config.js:

      {
        test: /\.node$/,
        loader: 'native-ext-loader',        
        //use: 'node-addon-loader',
        //use: 'node-loader',
        options: {
          //rewritePath: path.resolve(__dirname, 'dist')
        },           
      }

even using native-ext-loader did not work for me, I am using electron forge though.

Why was this closed? I also have the issue...

native-ext-loader did not work for me.

[email protected], same problem

native-ext-loader worked for me

native-ext-loader did not work for me, too....

@Pmjin What did you end up doing ? I have a similar issue with Windows only on certain machines.

Was this page helpful?
0 / 5 - 0 ratings