From 043f835e045f976ff1fa46181e5cce04aff2b251 Mon Sep 17 00:00:00 2001 From: Ylian Saint-Hilaire Date: Sun, 15 Mar 2020 11:35:33 -0700 Subject: [PATCH] Fixed npm path issue when upgrading MeshCentral. --- meshcentral.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/meshcentral.js b/meshcentral.js index c250dff8..44cca64d 100644 --- a/meshcentral.js +++ b/meshcentral.js @@ -2314,7 +2314,7 @@ function InstallModule(modulename, func, tag1, tag2) { // Get the working directory 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, '../..'); } - child_process.exec(`npm install --no-optional ${modulename}`, { maxBuffer: 512000, timeout: 120000, cwd: parentpath }, function (error, stdout, stderr) { + child_process.exec(npmpath + ` install --no-optional ${modulename}`, { maxBuffer: 512000, timeout: 120000, cwd: parentpath }, function (error, stdout, stderr) { if ((error != null) && (error != '')) { 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'); process.exit(); @@ -2334,6 +2334,7 @@ var serverWarnings = []; function addServerWarning(msg, print) { serverWarnings.push(msg); if (print !== false) { console.log("WARNING: " + msg); } } // Load the really basic modules +var npmpath = 'npm'; var meshserver = null; var childProcess = null; var previouslyInstalledModules = {}; @@ -2349,6 +2350,21 @@ function mainStart() { // Parse inbound arguments var args = require('minimist')(process.argv.slice(2)); + // Setup the NPM path + if (args.npmpath == null) { + try { + var xnodepath = process.argv[0]; + var xnpmpath = require('path').join(require('path').dirname(process.argv[0]), 'npm'); + if (require('fs').existsSync(xnodepath) && require('fs').existsSync(xnpmpath)) { + if (xnodepath.indexOf(' ') >= 0) { xnodepath = '"' + xnodepath + '"'; } + if (xnpmpath.indexOf(' ') >= 0) { xnpmpath = '"' + xnpmpath + '"'; } + if (require('os').platform() == 'win32') { npmpath = xnpmpath; } else { npmpath = (xnodepath + ' ' + xnpmpath); } + } + } catch (ex) { console.log(ex); } + } else { + npmpath = args.npmpath; + } + // Get the server configuration var config = getConfig(false); if (config == null) { process.exit(); }