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

Commit 03c5a27

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

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

lib/cv.js

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
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')
4+
const isElectron = process.versions.hasOwnProperty('electron')
5+
const packagePath = 'node_modules/opencv4nodejs-prebuilt'
56

6-
const logDebug = process.env.OPENCV4NODES_DEBUG_REQUIRE ? require('npmlog').info : () => {}
7+
const debugOrRelease = process.env.BINDINGS_DEBUG ? 'Debug' : 'Release'
8+
const binaryName = 'opencv4nodejs.node'
9+
let requirePath
10+
11+
let requireFunc
12+
if (isElectron) {
13+
// 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.
14+
const electron = require("electron")
15+
const appPath = (electron.app || electron.remote.app).getAppPath()
16+
requirePath = path.resolve(appPath, packagePath, `build/${debugOrRelease}/${binaryName}`)
17+
// requiring a .node file fails with webpack. __non_webpack_require__ tells webpack not to resolve the dependency
18+
requireFunc = __non_webpack_require__
19+
} else {
20+
requirePath = path.join(__dirname, `../build/${debugOrRelease}/${binaryName}`)
21+
requireFunc = require
22+
}
723

24+
const logDebug = process.env.OPENCV4NODES_DEBUG_REQUIRE ? require('npmlog').info : () => {}
825
let cv = null
926
try {
1027
logDebug('require', 'require path is ' + requirePath)
11-
cv = require(requirePath);
28+
cv = requireFunc(requirePath);
1229
} catch (err) {
1330
logDebug('require', 'failed to require cv with exception: ' + err.toString())
1431
throw err

0 commit comments

Comments
 (0)