1

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 Answers 3

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

Sign up to request clarification or add additional context in comments.

5 Comments

Do you run this script directly in the app? So that on every startup the script is executed, or is there a setting in the azure portal?
No additional settings on Portal, I just run the script directly in my app. @MichaelEckhart-Wöllkart
Thx for the fast response. But this means that the startup time of the app increases a lot, right?
Yes it would cause the startup time increase...But not much.@MichaelEckhart-Wöllkart
Thx, maybe i found a better solution, if you want you can take a look at my answer.
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();

1 Comment

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");"
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

3 Comments

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.
Take a look at AppService docs . 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
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.