Fixed npm path issue when upgrading MeshCentral.

Ylian Saint-Hilaire committed Mar 15, 2020 at 11:35 UTC 50858a9c96a95b050abb34a1bbe01d463131cc59
1 file changed +17 -1
meshcentral.js
+17 -1
@@ -2314,7 +2314,7 @@ function InstallModule(modulename, func, tag1, tag2) {
2314 // Get the working directory
2315 if ((__dirname.endsWith('/node_modules/meshcentral')) || (__dirname.endsWith('\\node_modules\\meshcentral')) || (__dirname.endsWith('/node_modules/meshcentral/')) || (__dirname.endsWith('\\node_modules\\meshcentral\\'))) { parentpath = require('path').join(__dirname, '../..'); }
2316
2317 - child_process.exec(`npm install --no-optional ${modulename}`, { maxBuffer: 512000, timeout: 120000, cwd: parentpath }, function (error, stdout, stderr) {
2317 + child_process.exec(npmpath + ` install --no-optional ${modulename}`, { maxBuffer: 512000, timeout: 120000, cwd: parentpath }, function (error, stdout, stderr) {
2318 if ((error != null) && (error != '')) {
2319 console.log('ERROR: Unable to install required module "' + modulename + '". MeshCentral may not have access to npm, or npm may not have suffisent rights to load the new module. Try "npm install ' + modulename + '" to manualy install this module.\r\n');
2320 process.exit();
@@ -2334,6 +2334,7 @@ var serverWarnings = [];
2334 function addServerWarning(msg, print) { serverWarnings.push(msg); if (print !== false) { console.log("WARNING: " + msg); } }
2335
2336 // Load the really basic modules
2337 +var npmpath = 'npm';
2338 var meshserver = null;
2339 var childProcess = null;
2340 var previouslyInstalledModules = {};
@@ -2349,6 +2350,21 @@ function mainStart() {
2350 // Parse inbound arguments
2351 var args = require('minimist')(process.argv.slice(2));
2352
2353 + // Setup the NPM path
2354 + if (args.npmpath == null) {
2355 + try {
2356 + var xnodepath = process.argv[0];
2357 + var xnpmpath = require('path').join(require('path').dirname(process.argv[0]), 'npm');
2358 + if (require('fs').existsSync(xnodepath) && require('fs').existsSync(xnpmpath)) {
2359 + if (xnodepath.indexOf(' ') >= 0) { xnodepath = '"' + xnodepath + '"'; }
2360 + if (xnpmpath.indexOf(' ') >= 0) { xnpmpath = '"' + xnpmpath + '"'; }
2361 + if (require('os').platform() == 'win32') { npmpath = xnpmpath; } else { npmpath = (xnodepath + ' ' + xnpmpath); }
2362 + }
2363 + } catch (ex) { console.log(ex); }
2364 + } else {
2365 + npmpath = args.npmpath;
2366 + }
2367 +
2368 // Get the server configuration
2369 var config = getConfig(false);
2370 if (config == null) { process.exit(); }