1

I am writing a c# program and I want to run (npm install) and (npm run build) from the c# code. I tried

public static Boolean BuildNpm()
        {
            Console.WriteLine("start process method?");
            var proc = new System.Diagnostics.Process();
            proc.StartInfo.WorkingDirectory="../../frontend/";
            proc.StartInfo.FileName = "/bin/bash";
            proc.Start();
            //npm install
            //npm run build
            //exit
            proc.WaitForExit();
            Console.WriteLine("Done?");
            return (proc.ExitCode == 0) ? true : false;
        }

I was able to create a shell that ask for command and I add the commands I want but I want the commands in the program automatically.

1 Answer 1

1

Why do you need to run bash to run the commands? Shouldn't it work to run npm directly from your program?

public static Boolean BuildNpm()
{
    Console.WriteLine("start process method?");
    var proc = new System.Diagnostics.Process();
    proc.StartInfo.WorkingDirectory="../../frontend/";
    proc.StartInfo.FileName = "/path/to/npm";
    proc.StartInfo.Arguments = "install";
    proc.Start();
    proc.WaitForExit();
    proc.StartInfo.Arguments = "run build";
    Console.WriteLine("Done?");
    return (proc.ExitCode == 0) ? true : false;
}

You should add some error handling if either execution of npm fails, as well.

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

2 Comments

I tried to do that but couldn't find the path to npm
If you run which npm that should return the path to npm. You can hard code that for now, but you'll need a better solution if you want other people to run your program as well.

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.