Node-API for .NET Interoperability
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
-
Error: Cannot find module 'node-api-dotnet'
cause The native module 'node-api-dotnet' could not be found or loaded, often due to compilation issues, incorrect Node.js version, or an architecture mismatch (e.g., x86 vs x64).fixEnsure Node.js and npm are up-to-date. Try running `npm rebuild` or `npm install` again. Verify that your system architecture matches the installed package binaries. Check Node-API compatibility matrix if issues persist. -
UnhandledPromiseRejectionWarning: .NET Exception: <Your .NET exception message>
cause An unhandled exception originated within the invoked .NET code and propagated back to the JavaScript promise chain without being caught.fixImplement robust error handling in your .NET code. In JavaScript, always use `.catch()` on promises returned by async .NET operations to handle .NET exceptions gracefully. -
TypeError: Cannot read properties of undefined (reading 'Console') or similar 'Cannot access X of undefined'
cause The specific .NET namespace (e.g., 'System') or type (e.g., 'Console') was not correctly exposed or accessed from the `node-api-dotnet` module due to incorrect path or casing.fixDouble-check the exact casing and nesting of the .NET types and namespaces you are trying to access. Consult the generated TypeScript definitions or the .NET API documentation for the correct path.
Warnings
- breaking The project is currently in 'Public Preview' status. This means there may be minor breaking API changes in future releases as the project matures and stabilizes.
- gotcha Performance overhead can occur with excessive synchronous calls or marshalling large amounts of data between JavaScript and .NET contexts. Optimize interop boundaries for efficiency.
Install
-
npm install node-api-dotnet -
yarn add node-api-dotnet -
pnpm add node-api-dotnet
Imports
- Node-API .NET root module
import dotnet from 'node-api-dotnet';
import * as dotnet from 'node-api-dotnet';
- System.Console (from .NET)
import { System } from 'node-api-dotnet'; const Console = System.Console;import * as dotnet from 'node-api-dotnet'; const Console = dotnet.System.Console;
- TypeScript types for .NET APIs
import { System } from 'node-api-dotnet'; // In a type-only contextimport type { System } from 'node-api-dotnet';
Quickstart
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.