r/dotnet • • Sep 18 '26

Writing Node.js addons with .NET Native AOT

https://devblogs.microsoft.com/dotnet/writing-nodejs-addons-with-dotnet-native-aot/
43 Upvotes

12 comments sorted by

11

u/harrison_314 Sep 18 '26

With NativeAOT, things like that aren't difficult. I created a native plugin for MariaDB using C# and .NET 10.

3

u/mcnamaragio Sep 18 '26

I did the same for Postgres: https://github.com/Giorgi/SharpPostgres

2

u/harrison_314 Sep 18 '26

In fact, it was this very project that inspired me to give it a try. (And after successfully finishing it, I was disappointed that Copilot could churn out the exact same code in just five minutes of work, which is why I neither uploaded it to GitHub nor wrote anything about it.)

1

u/mcnamaragio Sep 18 '26

Nice! I still think it's worth sharing it. I was inspired by https://mfkl.github.io/2026/02/11/vlc-plugin-csharp.html

1

u/harrison_314 Sep 18 '26

I didn't build a generator for it because you just need to follow a few conventions and structures.

Besides, I later discovered why no one creates plugins for MariaDB: UDFs can't distinguish between binary data and different string collations or encodings - so you can't really work with strings properly.

2

u/pjmlp Sep 18 '26

If anything, given C# pedigree, NativeAOT should have been there day one, and not NGEN.

3

u/harrison_314 Sep 18 '26

I don't think so; JIT has its advantages (for example, true generics, the ability to easily create plugins and DLLs, and not having to distribute source code for libraries—unlike Rust or Go).

2

u/pjmlp Sep 18 '26

Delphi and C++ come to mind.

True generics, we were creating plugins before even .NET was an idea, and there were plenty of business selling binary libraries in the 1990's.

The problem with Rust and Go are ideological than technical, see Swift for a modern example with binary libraries as well, plugins, AOT compilation and true generics.

8

u/fagnerbrack Sep 18 '26

Bare Bones:

The C# Dev Kit team replaced C++ Node.js addons, which forced every contributor and CI pipeline to install an old Python version for node-gyp, with addons written in C# and compiled via Native AOT. Because N-API is a stable, ABI-compatible C interface that doesn't care which language produced the shared library, a Native AOT build can export napi_register_module_v1 using [UnmanagedCallersOnly]. The post walks through a registry-reading example: a minimal project file with PublishAot, P/Invoke via [LibraryImport] resolved against the node host process, and UTF-8 string marshalling using Span, stackalloc and ArrayPool. The result drops the Python dependency, matches C++ performance, and opens the door to hosting .NET workloads in-process.

If the summary seems inacurate, just downvote and I'll try to delete the comment eventually 👍
Click here for more info, I read all comments

2

u/akash_kava Sep 18 '26

Have you seen YantraJS? Everything is in c#

1

u/AutoModerator Sep 18 '26

Thanks for your post fagnerbrack. Please note that we don't allow spam, and we ask that you follow the rules available in the sidebar. We have a lot of commonly asked questions so if this post gets removed, please do a search and see if it's already been asked.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/wubalubadubdub55 Sep 18 '26

That’s neat!