|  | 
| 1 | 1 | const path = require('path'); | 
| 2 | 2 | const { resolvePath } = require('./commons'); | 
| 3 | 3 | 
 | 
| 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' | 
| 7 | 6 | 
 | 
|  | 7 | +const debugOrRelease = process.env.BINDINGS_DEBUG ? 'Debug' : 'Release' | 
|  | 8 | +const binaryName = 'opencv4nodejs.node' | 
|  | 9 | +let requirePath | 
| 8 | 10 | let cv = null | 
|  | 11 | +let requireFunc | 
|  | 12 | +const logDebug = process.env.OPENCV4NODES_DEBUG_REQUIRE ? require('npmlog').info : () => {} | 
| 9 | 13 | 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 | +  } | 
| 12 | 27 | } catch (err) { | 
| 13 | 28 |   logDebug('require', 'failed to require cv with exception: ' + err.toString()) | 
| 14 | 29 |   throw err | 
|  | 
0 commit comments