惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
J
Java Code Geeks
Martin Fowler
Martin Fowler
Microsoft Azure Blog
Microsoft Azure Blog
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
人人都是产品经理
人人都是产品经理
有赞技术团队
有赞技术团队
爱范儿
爱范儿
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
博客园 - 【当耐特】
Y
Y Combinator Blog
Last Week in AI
Last Week in AI
MongoDB | Blog
MongoDB | Blog
G
Google Developers Blog
博客园 - 三生石上(FineUI控件)
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
大猫的无限游戏
大猫的无限游戏
罗磊的独立博客
The Cloudflare Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
V2EX
博客园 - 司徒正美

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Writing Node.js Addons with .NET Native AOT: A Complete G...
Vikrant Baga · 2026-04-29 · via DEV Community

Ever wished you could write Node.js native addons in C# instead of C++? .NET Native AOT makes it possible—and practical.

Writing Node.js Addons with .NET Native AOT


The Problem with Traditional Node.js Addons

If you've ever built a Node.js native addon, you know the pain:

  • C++ required: Even for simple functionality, you need C++ knowledge
  • node-gyp complexity: Build system that's notoriously finicky
  • Python dependency: Requires specific Python versions (often outdated)
  • Cross-platform headaches: Different build configurations per OS

For the Microsoft C# Dev Kit team, these friction points added unnecessary complexity to their development workflow. They needed Windows Registry access from their VS Code extension—but the C++ tooling didn't align with their .NET-centric stack.

The Solution: .NET Native AOT

What if you could write Node.js addons in C#? With .NET Native AOT, you can. The C# Dev Kit team replaced their C++ addon with a C# implementation—and it worked beautifully.

Here's why this approach is groundbreaking:

  1. No Python required - Just the .NET SDK
  2. Modern C# features - Type safety, async/await, LINQ
  3. Cross-platform - Works on Windows, Linux, and macOS
  4. Smaller toolchain - One SDK for everything

How It Works: N-API + Native AOT

The Magic Ingredient: N-API

N-API (Node-API) is a stable, ABI-compatible C API for building Node.js addons. The key insight: N-API doesn't care what language you use.

As long as your shared library exports the napi_register_module_v1 function and uses the correct C calling convention, Node.js will load it. This makes Native AOT a perfect fit.

Native AOT Compilation

.NET Native AOT compiles your C# code directly to native machine code, producing:

  • Windows: .dll shared library
  • Linux: .so shared library
  • macOS: .dylib shared library

Node.js treats these as native addons (rename to .node extension).


Step-by-Step Implementation

1. Create the Project

Create a new console app with these properties:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
    <PublishAot>true</PublishAot>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
  </PropertyGroup>
</Project>

Enter fullscreen mode Exit fullscreen mode

  • PublishAot enables Native AOT compilation
  • AllowUnsafeBlocks needed for pointer interop with N-API

2. Define the Module Entry Point

Node.js expects your shared library to export napi_register_module_v1. We use UnmanagedCallersOnly:

public static unsafe partial class RegistryAddon
{
    [UnmanagedCallersOnly(
        EntryPoint = "napi_register_module_v1",
        CallConvs = [typeof(CallConvCdecl)])]
    public static nint Init(nint env, nint exports)
    {
        Initialize();

        // Register JavaScript functions
        RegisterFunction(
            env,
            exports,
            "readStringValue"u8,
            &ReadStringValue);

        return exports;
    }
}

Enter fullscreen mode Exit fullscreen mode

Key points:

  • UnmanagedCallersOnly tells the AOT compiler to export the method
  • EntryPoint specifies the function name Node.js looks for
  • CallConvs defines the calling convention (cdecl for most platforms)
  • nint is the native-sized integer equivalent to intptr_t

3. Resolve N-API Functions Dynamically

N-API functions are exported by node.exe itself. We need to resolve them at runtime:

private static void Initialize()
{
    NativeLibrary.SetDllImportResolver(
        Assembly.GetExecutingAssembly(),
        ResolveDllImport);

    static nint ResolveDllImport(
        string libraryName,
        Assembly assembly,
        DllImportSearchPath? searchPath)
    {
        if (libraryName is not "node")
            return 0;

        // Resolve from the host process (Node.js)
        return NativeLibrary.GetMainProgramHandle();
    }
}

Enter fullscreen mode Exit fullscreen mode

4. Define N-API Function Imports

Use LibraryImport (source-generated P/Invoke) for better performance:

private static partial class NativeMethods
{
    [LibraryImport("node", EntryPoint = "napi_create_string_utf8")]
    internal static partial Status CreateStringUtf8(
        nint env, ReadOnlySpan<byte> str, nuint length, out nint result);

    [LibraryImport("node", EntryPoint = "napi_create_function")]
    internal static unsafe partial Status CreateFunction(
        nint env, ReadOnlySpan<byte> utf8name, nuint length,
        delegate* unmanaged[Cdecl]<nint, nint, nint> cb,
        nint data, out nint result);

    [LibraryImport("node", EntryPoint = "napi_get_cb_info")]
    internal static unsafe partial Status GetCallbackInfo(
        nint env, nint cbinfo, ref nuint argc,
        Span<nint> argv, nint* thisArg, nint* data);
}

Enter fullscreen mode Exit fullscreen mode

Why LibraryImport?

  • Source-generated (faster than reflection-based DllImport)
  • Trimming-compatible out of the box
  • Better error messages at compile time

5. Marshal Strings Between JavaScript and C

N-API uses UTF-8 strings. Here's how to read a string argument from JavaScript:

private static unsafe string? GetStringArg(nint env, nint cbinfo, int index)
{
    nuint argc = (nuint)(index + 1);
    Span<nint> argv = stackalloc nint[index + 1];
    NativeMethods.GetCallbackInfo(env, cbinfo, ref argc, argv, null, null);

    if ((int)argc <= index)
        return null;

    // Get the UTF-8 byte length
    NativeMethods.GetValueStringUtf8(env, argv[index], null, 0, out nuint len);

    // Allocate buffer (stack for small, pool for large)
    int bufLen = (int)len + 1;
    byte[]? rented = null;
    Span<byte> buf = bufLen <= 512
        ? stackalloc byte[bufLen]
        : (rented = ArrayPool<byte>.Shared.Rent(bufLen));

    try
    {
        fixed (byte* pBuf = buf)
            NativeMethods.GetValueStringUtf8(env, argv[index], pBuf, len + 1, out _);

        return Encoding.UTF8.GetString(buf[..(int)len)]);
    }
    finally
    {
        if (rented is not null)
            ArrayPool<byte>.Shared.Return(rented);
    }
}

Enter fullscreen mode Exit fullscreen mode

Performance optimizations:

  • stackalloc for small strings (< 512 bytes) - zero heap allocation
  • ArrayPool for larger strings - reusable buffers
  • Span<T> for memory-safe operations
  • No unnecessary string conversions

6. Implement Your Exported Function

With the plumbing in place, implementing actual functionality is straightforward:

[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
private static nint ReadStringValue(nint env, nint info)
{
    try
    {
        var keyPath = GetStringArg(env, info, 0);
        var valueName = GetStringArg(env, info, 1);

        if (keyPath is null || valueName is null)
        {
            ThrowError(env, "Expected two string arguments: keyPath, valueName");
            return 0;
        }

        // Your .NET logic here
        using var key = RegistryKey.OpenBaseKey(
            keyPath, RegistryHive.LocalMachine, RegistryView.Registry64);

        return key?.GetValue(valueName) is string value
            ? CreateString(env, value)
            : GetUndefined(env);
    }
    catch (Exception ex)
    {
        // CRITICAL: Always handle exceptions to avoid crashing Node.js
        ThrowError(env, $"Registry read failed: {ex.Message}");
        return 0;
    }
}

Enter fullscreen mode Exit fullscreen mode

Important: Exception handling is critical! Unhandled exceptions in UnmanagedCallersOnly methods will crash the Node.js process.

7. Call from TypeScript

Build your project and rename the output:

dotnet publish -c Release -r win-x64
# Result: RegistryAddon.dll
# Rename to: RegistryAddon.node

Enter fullscreen mode Exit fullscreen mode

Then use it in TypeScript:

// Define the interface
interface RegistryAddon {
    readStringValue(keyPath: string, valueName: string): string | undefined;
}

// Load the native module
const registry = require('./native/win32-x64/RegistryAddon.node') as RegistryAddon;

// Call your C# function!
const sdkPath = registry.readStringValue(
    'SOFTWARE\\dotnet\\Setup\\InstalledVersions\\x64\\sdk',
    'InstallLocation');

console.log('SDK Path:', sdkPath);

Enter fullscreen mode Exit fullscreen mode


Performance Comparison

According to Microsoft's C# Dev Kit team, the .NET Native AOT approach:

Metric C++ Addon .NET Native AOT Difference
Memory Lower Slightly higher Negligible in long-running processes
Startup Instant Fast (AOT compiled) Minimal
Performance Baseline Comparable No meaningful difference
Binary Size Smaller Larger Tradeoff for tool simplicity

Key insight: For typical addon workloads (string marshalling, registry access), performance is essentially identical. The GC overhead is negligible in long-running processes like VS Code extensions.


Real-World Benefits

Simplified Development

Before (C++ with node-gyp):

Requirements: C++, Python 2.x, node-gyp, platform-specific build tools

Enter fullscreen mode Exit fullscreen mode

After (C# with Native AOT):

Requirements: .NET SDK, Node.js

Enter fullscreen mode Exit fullscreen mode

Cross-Platform Support

The same C# code works on Windows, Linux, and macOS. Build once, deploy everywhere:

# Windows
dotnet publish -r win-x64

# Linux
dotnet publish -r linux-x64

# macOS
dotnet publish -r osx-x64

Enter fullscreen mode Exit fullscreen mode

Modern Language Features

Get access to the full .NET ecosystem:

  • Async/await for I/O operations
  • LINQ for data processing
  • Records and pattern matching
  • Strong typing and IntelliSense
  • Comprehensive error handling

Common Pitfalls

1. Exception Handling

Problem: Unhandled exceptions crash Node.js
Solution: Always wrap logic in try-catch, forward errors to JavaScript

2. String Encoding

Problem: Encoding issues, buffer overruns
Solution: Use UTF-8 consistently, proper buffer sizing

3. Function Pointers

Problem: C# delegates don't work with N-API callbacks
Solution: Use delegate* unmanaged[Cdecl]<...> syntax

4. Threading

Problem: N-API functions must be called from Node.js main thread
Solution: Keep addon single-threaded, use async patterns if needed

5. Memory Leaks

Problem: Forgetting to return ArrayPool buffers
Solution: Use try-finally blocks


When to Use This Approach

✅ Great For:

  • Cross-platform Node.js tools
  • Performance-critical operations
  • Teams with .NET expertise
  • Replacing Python dependencies
  • Consolidating toolchains

❌ Not Ideal For:

  • Very simple addons (use JavaScript instead)
  • Addons requiring extensive Node.js API surface
  • Projects where C++ expertise is readily available
  • Extremely memory-constrained environments

Getting Started

Prerequisites

  • .NET 10 SDK (or .NET 11 Preview)
  • Node.js 18+ (LTS version)
  • Basic C# and Node.js knowledge

Project Template

# Create new console app
dotnet new console -n MyNodeAddon

# Edit .csproj to add:
# <PublishAot>true</PublishAot>
# <AllowUnsafeBlocks>true</AllowUnsafeBlocks>

Enter fullscreen mode Exit fullscreen mode

Publishing

# Build for target platform
dotnet publish -c Release -r win-x64

# Rename output for Node.js
mv bin/Release/net10.0/win-x64/publish/MyNodeAddon.dll \
   bin/Release/net10.0/win-x64/publish/MyNodeAddon.node

Enter fullscreen mode Exit fullscreen mode


Conclusion

.NET Native AOT opens an exciting new frontier for Node.js addon development. By leveraging N-API's language-agnostic design, you can write native addons in C# while enjoying:

  • ✅ Simpler toolchain (no Python dependency)
  • ✅ Modern language features
  • ✅ Cross-platform compatibility
  • ✅ Comparable performance to C++

The Microsoft C# Dev Kit team has proven this approach works in production. Whether you're building developer tools, performance-critical operations, or just want to unify your tech stack, .NET Native AOT for Node.js addons is worth serious consideration.

Resources


This post is based on the official Microsoft announcement from April 15, 2026, written by Drew Noakes, Principal Software Engineer at Microsoft.

Follow me on LinkedIn for more .NET and JavaScript content!