---
name: node-ffi
description: "Calls C functions in shared libraries directly from JavaScript with the built-in node:ffi, with no C++ addon. Use to call libc, libsqlite3 or a vendor .so/.dylib/.dll from Node.js, pass a JS callback to native code, or read and write native memory. Use it instead of ffi-napi, node-ffi, or an addon written for one function. Covers ffi.dlopen, DynamicLibrary, getFunction, registerCallback, toFunction, the pointer and memory helpers and --allow-ffi. Node.js 26.1+ (not on v24)."
title: node-ffi
canonical_url: https://skilld.dev/gh/jasnell/node-skills/node-ffi
last_updated: 2026-11-19T20:15:43.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> Supporting files, fetch one when the Skill refers to it: [examples/libc-calls.mjs](https://skilld.dev/api/skills-raw/jasnell/node-skills/node-ffi/examples/libc-calls.mjs), [examples/memory-callbacks.mjs](https://skilld.dev/api/skills-raw/jasnell/node-skills/node-ffi/examples/memory-callbacks.mjs), [examples/permissions.mjs](https://skilld.dev/api/skills-raw/jasnell/node-skills/node-ffi/examples/permissions.mjs), [meta.json](https://skilld.dev/api/skills-raw/jasnell/node-skills/node-ffi/meta.json), [rules/api.md](https://skilld.dev/api/skills-raw/jasnell/node-skills/node-ffi/rules/api.md), [rules/call-paths-safety.md](https://skilld.dev/api/skills-raw/jasnell/node-skills/node-ffi/rules/call-paths-safety.md).
>
> If the user asked to install this Skill, run `npx skilld install jasnell/node-skills/node-ffi`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/node-ffi`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/jasnell/node-skills/node-ffi) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/node-ffi`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/node-ffi --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/node-ffi --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# node:ffi: call native libraries without an addon

| Feature | Versions | Stability | Flag |
| --- | --- | --- | --- |
| `node:ffi` (`dlopen`, `DynamicLibrary`, memory helpers) | v26.1.0 | 1 | none; `--no-experimental-ffi` disables |
| `--allow-ffi` (permission model) | v26.1.0 | 1.1 | with `--permission` |
| `getCurrentEventLoop()` | v26.6.0 | 1 | none |
| `dlopen` of a path inside a mounted `node:vfs` | v26.10.0 | 1 | none |
| `library.toFunction()` | main only (no YAML; absent in v26.13.0) | 1 | none |

Not on v24. Present only in builds with FFI support (the official v26 builds
have it). Importing it emits an `ExperimentalWarning`.

> **Experimental API.** Before relying on these details, run the
> `node-experimental-check` skill's probe (or `node --version` plus a
> feature-detect one-liner) and check the docs for *the installed
> version*: `https://nodejs.org/docs/<version>/api/<module>.html`.
> If the installed version disagrees with this skill, the installed
> version wins.

## Call a C function

```js
import { dlopen } from 'node:ffi';
{
  using h = dlopen('libc.so.6', {          // Linux glibc; NOT 'libc.so' (linker script)
    strlen: { arguments: ['string'], return: 'uint64' },
    abs:    { arguments: ['int32'],  return: 'int32' },
  });
  h.functions.strlen('héllo');             // 6n: 64-bit returns are always bigint
  h.functions.abs(-5);                     // 5
}                                          // library closed here
```

`dlopen(null, …)` resolves from the process image (not on Windows). Build
names portably with `` `libfoo.${ffi.suffix}` `` (`so`/`dylib`/`dll`).

## Pointers and callbacks

```js
const p = malloc(16);                      // 'pointer' returns are bigint
ffi.exportString('hi', p, 16); ffi.toString(p); free(p);
const cmp = lib.registerCallback({ arguments: ['pointer','pointer'], return: 'int32' },
  (a, b) => ffi.getInt32(a) - ffi.getInt32(b));
qsort(int32Array, n, 4, cmp); lib.unregisterCallback(cmp);
```

## Pitfalls

- No coercion: `abs(1.5)`, `abs('5')`, `abs(5n)` and `bool` `true` throw
  `ERR_INVALID_ARG_VALUE`. Pass `0`/`1` for `bool`.
- A `'string'` **return** is a bigint pointer. Decode it with `ffi.toString()`.
- Wrong signatures are not detected. Declaring `bool` for a C `int` truncates
  it, and a wrong pointer crashes the process.
- A callback that throws **aborts the process**. Callbacks must be sync and
  run on the creating thread. A `'function'` argument needs a
  `registerCallback` pointer, not a JS function.
- Calls are synchronous on the JS thread. A slow C call blocks the event loop.
- `toBuffer(p, n, false)` views and `getRawPointer()` dangle after `free()`
  or GC.
- `process.permission.drop('ffi')` does not stop calls or `getFunction()` on
  an already-open library. Only new libraries, `toFunction`, and memory
  helpers are denied.
- No structs by value, no calling-convention choice, fixed signatures only.

## Don't

- Don't install `ffi-napi`, `node-ffi`, `ref-napi` or `koffi` for simple
  calls on 26.1+.
- Don't write a C++ addon just to call one C function.
- Do still use Node-API/an addon when you need structs by value, threadpool
  work, native objects, v24 support, or a vetted safe API.

## Reference

- [rules/api.md](https://skilld.dev/api/skills-raw/jasnell/node-skills/node-ffi/rules/api.md): read for type names, every DynamicLibrary method, callbacks' lifetime rules, and memory helpers.
- [rules/call-paths-safety.md](https://skilld.dev/api/skills-raw/jasnell/node-skills/node-ffi/rules/call-paths-safety.md): read for performance (Fast API/shared-buffer/generic paths, arg limits), permissions, and the full safety list.
