Node-API for .NET Interoperability

0.9.19 · active · verified Sun Apr 19

node-api-dotnet is a JavaScript (Node.js) library that provides high-performance, in-process interoperability with .NET applications. It enables seamless bi-directional communication, allowing JavaScript applications to load .NET assemblies and invoke .NET APIs, and conversely, enabling .NET applications to interact with JavaScript packages and APIs. The package is currently in 'Public Preview' (version 0.9.19), indicating active development with potential for minor breaking API changes in future releases. It differentiates itself by leveraging Node-API (N-API), ensuring broad compatibility across Node.js versions without recompilation and support for other Node-API compatible JavaScript runtimes. Key features include TypeScript type-definition generation for .NET APIs, robust async/await support for .NET Tasks and JavaScript Promises, and efficient handling of data streams, providing a more integrated and performant alternative to inter-process communication solutions for mixed-language applications.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates importing the .NET interop module, calling a static .NET method, accessing a static property, and instantiating/using a .NET class (StringBuilder) from JavaScript with TypeScript.

import * as dotnet from 'node-api-dotnet'; // Using ESM for modern Node.js & TypeScript

// 1. Accessing a static .NET class and method
const Console = dotnet.System.Console;
Console.WriteLine('Hello from .NET via JavaScript!');

// 2. Accessing a static property (e.g., .NET CLR Version)
// System.Environment.Version is a common way to get CLR version in .NET
const clrVersion = dotnet.System.Environment.Version.ToString();
console.log(`\n.NET CLR Version: ${clrVersion}`);

// 3. Creating and using an instance of a .NET class (e.g., System.Text.StringBuilder)
const StringBuilder = dotnet.System.Text.StringBuilder;
const sb = new StringBuilder('Initial string');
sb.Append(' appended from JS!');
sb.Append(`\nCurrent UTC Time: ${new Date().toISOString()}`);
const finalString = sb.ToString();
console.log(`\nStringBuilder output:\n${finalString}`);

// Note: Async operations are also supported, e.g., for File I/O with .NET Tasks.
// This example focuses on synchronous interop for brevity.

view raw JSON →