The C# Dev Kit team replaced C++ Node.js addons (built with node-gyp and requiring Python) with C# using .NET Native AOT. The post walks through building a Node.js native addon in C# using N-API, covering the project setup, module entry point with [UnmanagedCallersOnly], P/Invoke via [LibraryImport] with a custom DLL resolver pointing to the host process, UTF-8 string marshalling using Span<T> and ArrayPool, and exposing functions to TypeScript. The result eliminated the Python dependency, simplified CI pipelines, and matched C++ performance while opening the door to hosting .NET logic in-process within Node.js.

10m read timeFrom devblogs.microsoft.com
Post cover image
Table of contents
How Node.js addons work Copy linkThe project file Copy linkThe module entry point Copy linkCalling N-API from .NET Copy linkMarshalling strings Copy linkImplementing an exported function Copy linkCalling our function from TypeScript Copy linkWhat about existing libraries? Copy linkWhat we gained Copy linkA footnote Copy linkSummary Copy link
3 Comments

Sort: