Electron-vue: Can not load native addon module in production

Created on 2 May 2018  ·  4Comments  ·  Source: SimulatedGREG/electron-vue

Describe the issue / bug.

#Can not load native addon module in production

How can I reproduce this problem?

# write a native addon and build for production

Tell me about your development environment.
  • Node version: 8.10
  • NPM version: 3.10.10
  • vue-cli version: (if necessary)
  • Operating System: Windows 10 64-bit

Hi,

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

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

and in src\main\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 my application in another PC, A Javascript error occurred in the main process:

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

It seems try to find the module with an absolute path which only exist in my development machine.

After I search for answers for hours I found something in the electron-builder 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

@cfy2015 The error "Cannot find module" can happen if the module is not compiled for your electron version. The error is misleading, because the file exists but is not recognized as a module for the specific version.

All 4 comments

I tried with that:
https://github.com/electron-userland/electron-packager/issues/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')
        },           
      }

for Electron applications, the rewritePath needs to remain undefined.

then in \src\main\index.js, require the native module with relative path:

var myaddon = require("../../node_modules/myaddon/build/Release/myaddon");

hello,i think i have same problem,but i can't even run dev,Can you tell me your specific steps?

Error: Cannot find module '../../node_modules/zoomsdk/build/Release/zoomsdk.node' at webpackEmptyContext (webpack:///./lib_sync?:2:10)

@cfy2015 The error "Cannot find module" can happen if the module is not compiled for your electron version. The error is misleading, because the file exists but is not recognized as a module for the specific version.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

haomehaode picture haomehaode  ·  3Comments

jt-wang picture jt-wang  ·  3Comments

okwangyu picture okwangyu  ·  3Comments

blackw212 picture blackw212  ·  3Comments

mvalim picture mvalim  ·  4Comments