From 93623c0fb9fe57a29b5644e1ec171c9d56ea4e7b Mon Sep 17 00:00:00 2001 From: Kristjan ESPERANTO <35647502+KristjanESPERANTO@users.noreply.github.com> Date: Wed, 6 Nov 2024 23:23:31 +0100 Subject: [PATCH] Fix issue with bundled modules (#302) --- node_helper.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/node_helper.js b/node_helper.js index 813c184..b7d695d 100644 --- a/node_helper.js +++ b/node_helper.js @@ -311,12 +311,22 @@ module.exports = NodeHelper.create(Object.assign({ }, loadModuleDefaultConfig(module, modulePath, lastOne) { - // function copied from MagicMirrorOrg (MIT) - let filename = path.resolve(modulePath + "/" + module.longname + ".js"); + const filename = path.resolve(modulePath + "/" + module.longname + ".js"); + const tempFilename = path.resolve("./modules/temp.js"); + try { fs.accessSync(filename, fs.F_OK); - const jsFile = require(filename); + // Add new line at the beginning of the file (this is necessary for module which are bundled) + const newContent = "const Log = console;const document = navigator = window = {};document.createElement = function() { return {}; };\n" + fs.readFileSync(filename, 'utf8'); + + // Write the new content to the temporary file + fs.writeFileSync(tempFilename, newContent, 'utf8'); + /* Defaults are stored when Module.register is called during require(filename); */ + const jsFile = require(tempFilename); + + // Unlink the temporary file + fs.unlinkSync(tempFilename); } catch (e) { if (e.code == "ENOENT") { console.error("ERROR! Could not find main module js file for " + module.longname);