0

I am hosting a .NET Core Application on MS Azure (on a Linux Service Plan) and I want to run some NodeJS code in the .NET Core Application. I did this a while ago on a Windows Service Plan, there it was working. Now I am trying with a Linux Plan and it is not working.

First I was trying to use "Jering.Javascript.NodeJS" and then also "INodeServices" from Microsoft (which is obsolete). But "node" was not found. I also tried to start directly a Process (Code below), but also not working. "node" is not found.

            var proc = new System.Diagnostics.Process
            {
                StartInfo = new System.Diagnostics.ProcessStartInfo
                {
                    FileName = "node",
                    Arguments = " -v",
                    RedirectStandardOutput = true
                }
            };
            result += "RUN: " + proc.StartInfo.FileName;
            proc.Start();
            var reader = proc.StandardOutput;

NodeJS is installed on the server and also the command works there but it seems that the .NET Core app is hosted as docker and does not have any access outside to run NodeJS. Image

3 Answers3

2

I found a useful information here.

The problem is that Node is not present in the container so it is necessary to have a script to install and start it before starting the app itself.

Reproduce: enter image description here

Here is my script:

//using System.Diagnostics;
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName = "bash";
//startinfo.FileName = "/etc/opt/nodejs/14.15.0/bin/node"; //it's no use even node package located here.
Process process = new Process();
process.StartInfo = startinfo;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
//install and start nodejs
process.StandardInput.WriteLine("apt-get install curl");
process.StandardInput.WriteLine("curl -sL https://deb.nodesource.com/setup_12.x | bash");
process.StandardInput.WriteLine("apt-get install -y nodejs");
//Run "node -v"
process.StandardInput.WriteLine("node -v");
string line = string.Empty;
        
while (!process.StandardOutput.EndOfStream)
{
     line = process.StandardOutput.ReadLine();
     _logger.LogInformation(line);
}
process.WaitForExit();
return string.Empty;

It works on my .net Core app based on Linux. enter image description here

Doris Lv
  • 3,083
  • 1
  • 5
  • 14
1

I think I found a better solution ;) In an app service you can mount a storage. In my case I mounted a storage, which contains the nodeJS lib. Azure Portal Screenshot

Now i can execute the following code:

string result = "";
var proc = new System.Diagnostics.Process
{
    StartInfo = new System.Diagnostics.ProcessStartInfo
    {
        FileName = "/externallibs/node/bin/node",
        Arguments = " -v",
        RedirectStandardOutput = true
    }
};
result += "RUN: " + proc.StartInfo.FileName;
proc.Start();
var reader = proc.StandardOutput;
return result +  reader.ReadToEnd();
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
  • That's wonderful! Thanks for sharing, Michael. – Doris Lv Jan 14 '21 at 01:33
  • You are welcome ;) If you add also something like that, you can also use just "node" and do not have to use the whole path. "Environment.SetEnvironmentVariable("PATH", Environment.GetEnvironmentVariable("PATH") + @":/externallibs/node/bin");" – Michael Eckhart-Wöllkart Jan 14 '21 at 05:32
0

You can create on azure portal an environment var named POST_BUILD_COMMAND with a command to fix your environment path.

Linux Service Plans runs on Oryx which is documented here

POST_BUILD_COMMAND=PATH=/usr/bin/node:$PATH
Jone Polvora
  • 2,184
  • 1
  • 22
  • 33
  • The problem was not the missing envrioment variable. The whole node js is not available with linux app services. But that can be fixed with my question below or using docker and install nodejs directly in the image. – Michael Eckhart-Wöllkart May 11 '22 at 06:58
  • Take a look at [AppService docs](https://learn.microsoft.com/en-us/azure/app-service/overview) . You can use Azure client on your app service to list all available runtimes with the command 'az webapp list-runtimes --linux` or `az webapp list-runtimes --os windows` – Jone Polvora May 19 '22 at 22:53
  • in a linux app service, there are dozens of runtimes available, include node. Take a look at the following list: "DOTNETCORE", "NODE", "PYTHON", "PHP", "RUBY", "JAVA", "JBOSS", "TOMCAT – Jone Polvora May 19 '22 at 22:55