Fixed module update for modules like firebase-admin.js

Ylian Saint-Hilaire committed Dec 15, 2024 at 00:24 UTC 22fc95926a8f4df0c124e5e57318258e374f5586
1 file changed +8 -4
meshcentral.js
+8 -4
@@ -4042,10 +4042,14 @@ function InstallModules(modules, args, func) {
4042 // Does the module need a specific version?
4043 if (moduleVersion) {
4044 var versionMatch = false;
4045 - try { versionMatch = (require(`${moduleName}/package.json`).version == moduleVersion) } catch (ex) { }
4046 - if (versionMatch == false) {
4047 - const packageJson = JSON.parse(require('fs').readFileSync(require('path').join(__dirname, 'node_modules', moduleName, 'package.json'), 'utf8'));
4048 - if (packageJson.version != moduleVersion) { throw new Error(); }
4045 + var modulePath = null;
4046 + // This is the first way to test if a module is already installed.
4047 + try { versionMatch = (require(`${moduleName}/package.json`).version == moduleVersion) } catch (ex) {
4048 + if (ex.code == "ERR_PACKAGE_PATH_NOT_EXPORTED") { modulePath = ("" + ex).split(' ').at(-1); } else { throw new Error(); }
4049 + }
4050 + // If the module is not installed, but we get the ERR_PACKAGE_PATH_NOT_EXPORTED error, try a second way.
4051 + if ((versionMatch == false) && (modulePath != null)) {
4052 + if (JSON.parse(require('fs').readFileSync(modulePath, 'utf8')).version != moduleVersion) { throw new Error(); }
4053 }
4054 } else {
4055 // For all other modules, do the check here.