Python ctypes for Node.js. A foreign function interface built with C++20, node-addon-api and libffi. If you know Python ctypes, you already know node-ctypes.
Development help is welcome: issues, pull requests and discussions are all appreciated.
Tip
If you find node-ctypes useful, consider ⭐ this project and why not ... Buy me a coffee 😄
npm install node-ctypesPrebuilt binaries ship for Windows, macOS 13.5+ and Linux (glibc 2.28+ or musl / Alpine), on x64 and ARM64 — wherever Node 24 runs.
import { CDLL, Structure, c_int32, c_size_t, c_char_p } from "node-ctypes";
// 1. Load a shared library
const libc = new CDLL(process.platform === "win32" ? "msvcrt" : null);
// 2. Declare a function signature
const strlen = libc.strlen;
strlen.argtypes = [c_char_p];
strlen.restype = c_size_t;
console.log(strlen("hello")); // 5n
// 3. Declare a struct
class Point extends Structure {
static _fields_ = [
["x", c_int32],
["y", c_int32],
];
}
const p = new Point(3, 4);
console.log(p.x, p.y); // 3 4To build from source, see Getting started.
| Operating system | x64 | ARM64 | Minimum version |
|---|---|---|---|
| Windows | ✅ | ✅ | |
| macOS | ✅ | ✅ | macOS 13.5 |
| Linux, glibc | ✅ | ✅ | glibc 2.28 |
| Linux, musl (Alpine) | ✅ | ✅ |
Node.js 24 or later. Details, testing and building from source are in Getting started.
- Getting started — installation, supported platforms, building from source
- Libraries and functions — loading libraries, declaring and calling functions, adapters
- Types — the C types, their sizes and JavaScript values, scalar subclasses
- Structures, unions and arrays — layouts, bit fields, packing, byte order
- Pointers and memory — pointers,
cast, shared buffers, native memory, Electron - Callbacks — C calling JavaScript, also from other threads
- Asynchronous calls —
callAsyncand theExecutor - Errors and results —
errno,GetLastError,errcheck, output parameters - Windows API —
wintypes - TypeScript — typed signatures and structures
- Python ctypes parity — supported behavior, differences, migrating from 1.x
- Coming from Koffi — the Koffi API mapped to node-ctypes
- Performance — comparisons with Koffi and how to run them
- Building, testing and internals — tests, native backends, release validation
Each example in examples/ has its own README; see the index.
- Electron + React — a native system clock through a sandboxed preload bridge, synchronized C-memory views and a packaged ASAR application
- Embedded Python — CPython through its C API: code, modules, callbacks, JavaScript objects by reference, shared memory, threads, asyncio and cancellation (Windows, macOS, Linux)
- Smart card (PC/SC) —
winscardon Windows,PCSC.frameworkon macOS,libpcscliteon Linux - Cryptoki (PKCS #11) — complete 3.2 declarations with 2.20–3.2 dispatch and a standalone SoftHSM demo: temporary token, login, RSA, ECDSA, AES and SHA-256
- Accessibility — macOS UI automation with the Accessibility API
- COM automation — shell shortcuts (
IShellLinkW) and Active Directory (IDirectorySearch) - Controls — a small Win32 application with the common controls
- LDAP — LDAPv3 paged search (
wldap32) - Registry —
setValue/getValue/openKey/deleteKey - System tray — tray icon + popup menu
MIT. Built with libffi, node-addon-api, and cmake-js.