Skip to content

Place NodeJS executables in your application's output directory

License

Notifications You must be signed in to change notification settings

JeringTech/Redist.NodeJS

Repository files navigation

Jering.Redist.NodeJS

Build Status License TODO publish package:
NuGet

Table of Contents

Overview
Installation
Usage
Projects Using this Package
Contributing
About

Overview

Jering.Redist.NodeJS places NodeJS executables in your application's output directory.

This package is automatically updated. Package versions correspond to NodeJS versions.

Installation

Using Package Manager:

PM> Install-Package Jering.Redist.NodeJS

Using .Net CLI:

> dotnet add package Jering.Redist.NodeJS

Usage

Where Do the Executables Go?

NodeJS executables for 4 kinds of operating systems are copied to the output directory of your project:

/<project path>/bin/<configuration>/<target framework>
├──<your application>.dll
|
├──miscellaneous dlls
|
└──/NodeJS
   |
   ├──/linux-x64
   |  └──node
   |
   ├──/osx-x64
   |  └──node
   |
   ├──/win-x64
   |  └──node.exe
   |
   └──/win-x86
      └──node.exe

Using the Executables

We recommend using Jering.Javascript.NodeJS to invoke javascript in NodeJS. Jering.Javascript.NodeJS starts and manages NodeJS processes for you, reusing them indefinitely to avoid the overhead of starting and killing NodeJS processes.

If you prefer to manage your own NodeJS process, here's how you can locate a suitable executable:

public Process CreateNodeJSProcess(string args)
{
    string runtimeIdentifier;
    bool isWindows;

    // Determine OS
    if (isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
    {
        runtimeIdentifier = "win-";
    }
    else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
    {
        runtimeIdentifier = "linux-";
    }
    else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
    {
        runtimeIdentifier = "osx-";
    }
    else
    {
        // Jering.Redist.NodeJS doesn't provide an executable suitable for the current machine
        return null;
    }

    // Determine architecture
    if (RuntimeInformation.OSArchitecture == Architecture.X64)
    {
        runtimeIdentifier += "x64";
    }
    else if (isWindows && RuntimeInformation.OSArchitecture == Architecture.X86)
    {
        runtimeIdentifier += "x86";
    }
    else
    {
        // Jering.Redist.NodeJS doesn't provide an executable suitable for the current machine
        return null;
    }

    // Create executable path
    string executablePath = Path.Combine(Directory.GetCurrentDirectory(), "NodeJS", runtimeIdentifier, "node");

    // Start process
    return Process.Start(executablePath, args);
}

Do note that logic in the example for starting a process is trivialized. You'd typically need at least exception and output (stdout/stderr) handling. Refer to Microsoft's documentation for the Process type for more information.

Projects Using this Package

Jering.Javascript.NodeJS - Invoke Javascript in NodeJS, from C#.

Contributing

Contributions are welcome!

About

Follow @JeringTech for updates and more.

About

Place NodeJS executables in your application's output directory

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •