Skip to content

MKI

Node.js just turned on calling C libraries by default

Node.js 26.9.0 enables node:ffi without a flag, so any script can now call native code. Here's what that changes and what it doesn't.

Node.js 26.9.0 shipped on September 16 with one change that’s easy to miss in a changelog and hard to miss in practice: node:ffi, the module for calling native functions without writing a compiled addon, is now enabled by default. Up to 26.8.2 you needed --experimental-ffi to touch it. From 26.9.0 on, import ffi from 'node:ffi' just works, and the flag that used to turn the module on now turns it off (Node.js 26.9.0 release notes).

The module itself isn’t new — it landed experimentally in Node 26.1.0 back in May. What changed on September 16 is the default. Matteo Collina’s pull request describes the intent plainly: “Enable node:ffi by default in builds with FFI support. Keep --experimental-ffi as a compatibility no-op and --no-experimental-ffi as an opt-out” (PR #65475). The module still logs an experimental warning on first load, and it’s still marked Stability 1 in the docs, but it no longer requires an opt-in flag to reach.

If your team has ever reached for node-ffi-napi, shelled out to a compiled binary, or written an N-API addon just to call one function in a system library, this is the thing that makes all three optional going forward.

What changed

The core capability: node:ffi lets JavaScript load a shared library (.so, .dylib, or .dll) and call its exported functions directly, with Node handling argument marshalling based on a declared signature.

import { dlopen, suffix } from 'node:ffi'
const { functions } = dlopen(`./mylib.${suffix}`, {
add_i32: { arguments: ['int32', 'int32'], return: 'int32' },
})
console.log(functions.add_i32(20, 22))

That’s the whole integration. No node-gyp, no compiler toolchain baked into your deploy image, no addon rebuild every time you bump the Node ABI version. The Node.js FFI documentation lists the supported type names (int8 through float64, pointer, string, buffer), the signature object shape, and a full DynamicLibrary class with getFunction(), registerCallback(), and explicit-resource-management support via using blocks — the returned handle disposes itself when it leaves scope, closing the library automatically.

The practical trigger for this piece is the default flip, but the surrounding release is worth a glance too: 26.9.0 also shipped Web Worker support in worker_threads (node:worker, PR #64894) and a new node:bench benchmarking module, though the latter was put back behind an --experimental-bench flag in the same release cycle after landing (PR #65920) — a reminder that “shipped this week” and “stable” aren’t the same claim, even within one changelog.

The part that actually matters for CI

The subtle risk here isn’t runtime behavior, it’s version pinning. If any dependency in your tree does a bare require('node:ffi') wrapped in a try/catch — a common defensive pattern for optional native bindings — that code used to fail silently on Node 26.0–26.8 without the flag, and now succeeds silently on 26.9+. That’s a behavior change with no corresponding error message, which is exactly the kind of thing that passes CI on the old pin and does something new on the new one.

If you maintain a package that ships an FFI-based fast path with a JS fallback, this is worth testing explicitly: pin a CI job to 26.8.x and one to 26.9.x and confirm both take the path you expect them to take.

Where the performance actually lands

Node’s own docs describe node:ffi as unsafe without saying much about how it performs relative to the alternatives it’s meant to replace. A benchmark published the day after release fills that gap with real numbers from a Linux box running 26.9.0: a trivial add_i32(a, b) call measured at 37.5–38.1ns per call, against 34.1–35.8ns for an equivalent compiled N-API addon and 2.2–3.0ns for the same operation in plain JS (Node.js 26.9 turns node:ffi on by default at 37 nanoseconds a call).

That’s roughly a 7–8% overhead versus a hand-written addon doing the identical work, and both are around fifteen times the cost of staying in JS for something this small. The gap is the price of marshalling arguments across the JS/native boundary on every call — paid whether you wrote C or just declared a signature in JavaScript.

The same benchmark found the opposite result once the workload shifted from many small calls to one call moving a lot of data: summing ten million float64 values through a pointer ran 15–20% faster via FFI than the equivalent JS for loop, because one call carries the whole buffer instead of amortizing per-call overhead ten million times over. A single call to a native fib(75) implementation, by contrast, came out statistically identical to the same loop written in JS — V8’s JIT compiles a tight loop about as fast as gcc -O2 does, so crossing into native code for one moderate computation buys nothing.

What this means for a real decision

The shape to look for is call count times payload size, not “is this in C.” If you already have a working native addon, node:ffi is not a reason to rewrite it — the 37ns-versus-35ns gap runs the wrong direction to justify that migration cost. Where it earns its place is the case a team actually runs into: an occasional need to call one function in an existing shared library — a codec, a compression routine, a vendor SDK shipped only as a .so — where writing and maintaining a full addon for that one call was never worth it. FFI turns that from “skip it or commit to a build pipeline” into “declare a signature and call it.”

Safety: what it catches and what it doesn’t

The docs call the module unsafe in the first paragraph, and that same benchmark tested exactly how unsafe by trying four ways to misuse it and getting three different outcomes.

Passing a Number where a uint64 argument is expected is rejected outright with a TypeError — you have to pass a BigInt, and Node won’t silently coerce a small integer for you. Calling a function with the wrong argument count is also caught explicitly. Both of those are real guardrails, and neither matches the “can crash the process” warning on its own.

Declaring the wrong return type is where the warning starts to earn its place: a function whose real return type is int64, declared as int32, produced no error and no warning — just a silently truncated value with no signal that anything went wrong. Passing a real pointer with an incorrect claimed length went further: a 10-element buffer told the call site it held ten million elements produced an immediate segmentation fault. The module validates argument count and basic scalar types; it does not validate pointer bounds or return-type correctness, and that’s the line worth internalizing before it goes anywhere near a code path handling buffer sizes derived from user input or a miscounted array.

Node’s Permission Model treats FFI as its own gate independent of the default-on change: running with --permission still requires an explicit --allow-ffi, and Node attaches its own warning to that flag — [PERM0003] SecurityWarning: The flag --allow-ffi must be used with extreme caution. It could invalidate the permission model — because native code loaded through FFI isn’t bound by any of the other restrictions a permission-restricted process sets up.

What we’d do about it

For most of the static sites and content-driven builds we ship, this changes nothing directly — Astro’s build pipeline and the Node runtime behind our hosting don’t call into node:ffi. But it’s relevant for two situations that come up in client work: build tooling with a native fast path (image processing, native compression, a vendored SDK), and any Node service we maintain that talks to a system library a customer already has installed.

Concretely, here’s what we’d change this week on anything Node-based:

First, audit package.json dependency trees for anything wrapping require('node:ffi') in a try/catch, and pin a CI matrix entry to a pre-26.9 patch version if you need to verify a fallback path still behaves as designed. Second, if a project has been avoiding a useful C library because writing and maintaining an N-API addon for one function wasn’t worth it, node:ffi changes that math — but only for genuinely occasional or bulk-data calls, not for anything in a hot per-request path where 37ns per call adds up. Third, if the project runs under --permission in production, confirm --allow-ffi is not silently present anywhere in the launch command; the Permission Model warning exists because that flag is meant to be a deliberate, audited decision, not a default.

None of this is urgent in the way a security patch is. It’s the kind of runtime default change that’s worth twenty minutes now so it doesn’t surface as a surprise in a dependency upgrade six months from now.

Sources