Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Commit

Permalink
Fix: Webpacked electron app can't load native module
Browse files Browse the repository at this point in the history
  • Loading branch information
stoefln committed Dec 1, 2021
1 parent bcd293b commit 03c5a27
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions lib/cv.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
const path = require('path');
const { resolvePath } = require('./commons');

const requirePath = path.join(__dirname, process.env.BINDINGS_DEBUG ? '../build/Debug/opencv4nodejs' : '../build/Release/opencv4nodejs')
const isElectron = process.versions.hasOwnProperty('electron')
const packagePath = 'node_modules/opencv4nodejs-prebuilt'

const logDebug = process.env.OPENCV4NODES_DEBUG_REQUIRE ? require('npmlog').info : () => {}
const debugOrRelease = process.env.BINDINGS_DEBUG ? 'Debug' : 'Release'
const binaryName = 'opencv4nodejs.node'
let requirePath

let requireFunc
if (isElectron) {
// in electron we are referencing the node module from a packed file in some static folder. So a relative path will be complicated. Use absolute path instead.
const electron = require("electron")
const appPath = (electron.app || electron.remote.app).getAppPath()
requirePath = path.resolve(appPath, packagePath, `build/${debugOrRelease}/${binaryName}`)
// requiring a .node file fails with webpack. __non_webpack_require__ tells webpack not to resolve the dependency
requireFunc = __non_webpack_require__
} else {
requirePath = path.join(__dirname, `../build/${debugOrRelease}/${binaryName}`)
requireFunc = require
}

const logDebug = process.env.OPENCV4NODES_DEBUG_REQUIRE ? require('npmlog').info : () => {}
let cv = null
try {
logDebug('require', 'require path is ' + requirePath)
cv = require(requirePath);
cv = requireFunc(requirePath);
} catch (err) {
logDebug('require', 'failed to require cv with exception: ' + err.toString())
throw err
Expand Down

0 comments on commit 03c5a27

Please sign in to comment.