Node.js v26.8.2 documentation
- Node.js v26.8.2
- Table of contents
- FFI
- Overview
- Type names
- Signature objects
ffi.suffixffi.dlopen(path[, definitions])ffi.dlclose(handle)ffi.dlsym(handle, symbol)- Class:
DynamicLibrarynew DynamicLibrary(path)library.pathlibrary.functionslibrary.symbolslibrary.close()library[Symbol.dispose]()library.getFunction(name, signature)library.getFunctions([definitions])library.getSymbol(name)library.getSymbols()library.registerCallback([signature,] callback)library.unregisterCallback(pointer)library.refCallback(pointer)library.unrefCallback(pointer)
- Calling native functions
- Primitive memory access helpers
ffi.toString(pointer)ffi.toBuffer(pointer, length[, copy])ffi.toArrayBuffer(pointer, length[, copy])ffi.exportString(string, pointer, length[, encoding])ffi.exportBuffer(buffer, pointer, length)ffi.exportArrayBuffer(arrayBuffer, pointer, length)ffi.exportArrayBufferView(arrayBufferView, pointer, length)ffi.getRawPointer(source)ffi.getCurrentEventLoop()- Safety notes
- FFI
- Index
- About this documentation
- Usage and example
- Assertion testing
- Asynchronous context tracking
- Async hooks
- Buffer
- C++ addons
- C/C++ addons with Node-API
- C++ embedder API
- Child processes
- Cluster
- Command-line options
- Console
- Crypto
- Debugger
- Deprecated APIs
- Diagnostics Channel
- DNS
- Domain
- Environment Variables
- Errors
- Events
- File system
- FFI
- Globals
- HTTP
- HTTP/2
- HTTPS
- Inspector
- Internationalization
- Iterable Streams API
- Modules: CommonJS modules
- Modules: ECMAScript modules
- Modules:
node:moduleAPI - Modules: Packages
- Modules: TypeScript
- Net
- OS
- Path
- Performance hooks
- Permissions
- Process
- Punycode
- Query strings
- Readline
- REPL
- Report
- Single executable applications
- SQLite
- Stream
- String decoder
- Test runner
- Timers
- TLS/SSL
- Trace events
- TTY
- UDP/datagram
- URL
- Utilities
- V8
- Virtual File System
- VM
- WASI
- Web Crypto API
- Web Streams API
- Worker threads
- Zlib
- Other versions
- Options
FFI#
Stability: 1 - Experimental
The node:ffi module provides an experimental foreign function interface for
loading dynamic libraries and calling native symbols from JavaScript.
This API is unsafe. Passing invalid pointers, using an incorrect symbol signature, or accessing memory after it has been freed can crash the process or corrupt memory.
To access it:
import ffi from 'node:ffi';const ffi = require('node:ffi');
This module is only available under the node: scheme in builds with FFI
support and is gated by the --experimental-ffi flag.
Building Node.js with node:ffi support is available via the bundled libffi on
platforms where libffi provides a compatible static backend, or via a
shared libffi using the --shared-ffi configure flag.
The unofficial GN build does not support node:ffi.
The following targets are not supported by bundled libffi:
s390x.mips,mipsel, andmips64elon targets other than FreeBSD, Linux, and OpenBSD.ppc64on Android, CloudABI, iOS, OpenHarmony, OS/400, Solaris, and Windows.
When using the Permission Model, FFI APIs are
restricted unless the --allow-ffi flag is provided.
Overview#
The node:ffi module exposes two groups of APIs:
- Dynamic library APIs for loading libraries, resolving symbols, and creating callable JavaScript wrappers.
- Raw memory helpers for reading and writing primitive values through pointers,
converting pointers to JavaScript strings,
Bufferinstances, andArrayBufferinstances, and for copying data back into native memory.
Type names#
FFI signatures use string type names.
Supported type names:
voidcharint8uint8int16uint16int32uint32int64uint64float32float64pointerstringbufferarraybufferfunction
Alternative spellings
i8forint8u8andboolforuint8i16forint16u16foruint16i32forint32u32foruint32i64forint64u64foruint64f32andfloatforfloat32f64anddoubleforfloat64ptrforpointerstrforstring
These type names are also exposed as constants on ffi.types:
ffi.types.VOID='void'ffi.types.POINTER='pointer'ffi.types.BUFFER='buffer'ffi.types.ARRAY_BUFFER