-
Notifications
You must be signed in to change notification settings - Fork 8
Description
It seems like you added support for automatic search for module inside node_modules.asar via addAsarToLookupPaths as in hooking Module._resolveLookupPaths - however not in app.asar which is the default bundle for electron(-builder) given that might also include node_modules.
In my case I'm calling child_process.fork inside an Electron-App for a background-server that I want to require node_modules and dependencies inside app.asar.
Furthermore I'm unsure if your code even properly works - I had to refactor it quite a bit to make it work.
See below example code - if you want I can refine this into a PR to add a configuration option to search in an array-list of files so that your current addAsarToLookupPaths can be replaced by a more generic utility for all cases.
Do you want a PR? :)
// top of your entryPoint.js or whatever module that you want to fork
const asar = require('asar-node');
const path = require('path');
const Module = require('module');
asar.register();
const baseDir = path.join(__dirname, "../");
const _orgResolveLookupPaths = Module._resolveLookupPaths;
(function hookModule () {
if(Module.isHooked) return;
function appendPaths (request, paths) {
let _possibleAppendedPaths = [path.join(baseDir, "/app.asar/", "/node_modules/", request)];
for (let i = 0; i < paths.length; i++) {
const _path = paths[i];
const isBaseDir = _path.indexOf(baseDir);
if(isBaseDir > -1 && _path.indexOf("app.asar") < 0) {
_possibleAppendedPaths.push(path.join(baseDir, "/app.asar/", _path.substr(isBaseDir+baseDir.length)))
}
}
return paths = paths.concat(_possibleAppendedPaths);
}
Module._resolveLookupPaths = function (request, parent) {
if(parent && parent.paths) {
parent.paths = appendPaths(request, parent.paths);
}
return _orgResolveLookupPaths.call(this, request, parent)
}
Module.isHooked = true
})();
//----- your regular dependencies afterwards
const express = require('express'); //will be loaded from ./app.asar/node_modules/express successfully