I have a WEB API application, done in ASP.NET framework 4.7, where I want to call an application in NodeJs using the command line, therefore, using the System.Diagnostic.Process.
The NodeJs application has been installed using
npm install -g mapshaper
In the development enviroment everything works fine, and I can even capture the output, but when I run on production server I get in the standard output the following message:
'mapshaper' is not recognized as an internal or external command,\r\noperable program or batch file.
(mapshaper is the nodejs application I want to run)
Please note that if I run the application, on the production server, from the command-line directly, it works perfectly.
And if I run from the API, instead of 'mapshaper', the command 'DIR' I get the directory list, therefore it works. I've also tried to run 'node -v' and I correctly get the installed node version:
c:\\windows\\system32\\inetsrv>node -v\r\nv10.16.3
Here is the code I use to call the cmd.exe, and insert the command to run:
string strOutput = "";
string path = HostingEnvironment.MapPath("~");
string fileName = "cmd.exe ";
string args = "mapshaper -h";
//args = "dir"; //IT WORKS
//args = "node -v"; //IT WORKS and I get the ver. installed as output
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
pProcess.StartInfo.FileName = fileName;
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.RedirectStandardError = true;
pProcess.StartInfo.RedirectStandardInput = true;
ConcurrentQueue<string> messages = new ConcurrentQueue<string>();
pProcess.ErrorDataReceived += (object se, DataReceivedEventArgs ar) =>
{
string data = ar.Data;
if (!string.IsNullOrWhiteSpace(data))
messages.Enqueue(data);
};
pProcess.OutputDataReceived += (object se, DataReceivedEventArgs ar) =>
{
string data = ar.Data;
if (!string.IsNullOrWhiteSpace(data))
messages.Enqueue(data);
};
pProcess.Start();
pProcess.StandardInput.WriteLine(args);
pProcess.StandardInput.Flush();
pProcess.StandardInput.Close();
pProcess.BeginErrorReadLine();
pProcess.BeginOutputReadLine();
while (!pProcess.HasExited)
{
string data = null;
if (messages.TryDequeue(out data))
strOutput+=data+"\r\n";
}
pProcess.WaitForExit(2000);
pProcess.Close();
So on production is only the command mapshaper that is not recognized when it's run from the above code (it works if it's run manually from command line).
Which could be the cause? Some permission to execute NodeJs on the server?
Please make sure that you have run the command npm install -g mapshaper
in the production server, npm install
will not install the global packages which in development server.
Also make sure that path linked with /npm packages dir, please run this script in your command line
path=%PATH%;%APPDATA%\npm