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

Commit e97ea64

Browse files
committed
Fix: Webpacked electron app can't load native module
1 parent bcd293b commit e97ea64

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

lib/cv.js

+20-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
11
const path = require('path');
22
const { resolvePath } = require('./commons');
33

4-
const requirePath = path.join(__dirname, process.env.BINDINGS_DEBUG ? '../build/Debug/opencv4nodejs' : '../build/Release/opencv4nodejs')
5-
6-
const logDebug = process.env.OPENCV4NODES_DEBUG_REQUIRE ? require('npmlog').info : () => {}
4+
const isElectron = process.versions.hasOwnProperty('electron')
5+
const packagePath = 'node_modules/opencv4nodejs-prebuilt'
76

7+
const debugOrRelease = process.env.BINDINGS_DEBUG ? 'Debug' : 'Release'
8+
const binaryName = 'opencv4nodejs.node'
9+
let requirePath
810
let cv = null
11+
let requireFunc
12+
const logDebug = process.env.OPENCV4NODES_DEBUG_REQUIRE ? require('npmlog').info : () => {}
913
try {
10-
logDebug('require', 'require path is ' + requirePath)
11-
cv = require(requirePath);
14+
if (isElectron) {
15+
// in electron we are referencing the node module from a packed file in some static folder. So relative path will be complicated. Use absolute path instead.
16+
const electron = require("electron")
17+
const appPath = (electron.app || electron.remote.app).getAppPath()
18+
requirePath = path.resolve(appPath, packagePath, `build/${debugOrRelease}/${binaryName}`)
19+
// requireing a .node file fails with webpack. __non_webpack_require__ tells webpack not to resolve the dependency
20+
logDebug('require', 'require path is ' + requirePath)
21+
cv = __non_webpack_require__(requirePath);
22+
} else {
23+
requirePath = path.join(__dirname, `../build/${debugOrRelease}/${binaryName}`)
24+
logDebug('require', 'require path is ' + requirePath)
25+
cv = require(requirePath);
26+
}
1227
} catch (err) {
1328
logDebug('require', 'failed to require cv with exception: ' + err.toString())
1429
throw err

0 commit comments

Comments
 (0)