node-ctypes - v2.17.1
    Preparing search index...

    node-ctypes - v2.17.1

    node-ctypes

    npm version npm downloads Build License: MIT

    Python ctypes for Node.js. A foreign function interface built with C++20, node-addon-api and aotffi (zero-JIT AOT C++20 + assembly backend). 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 :star: this project and why not ... Buy me a coffee :smile:


    npm install node-ctypes
    

    Prebuilt 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 4

    To build from source, see Getting started.

    Warning

    JavaScript runs on one thread. A C function called synchronously blocks it, so a threadSafeCallback invoked by another thread cannot run until that call returns. Never wait synchronously (WaitForSingleObject, pthread_join, …) for a thread that calls a threadSafeCallback: the two threads would wait for each other. Make such waits asynchronous with callAsync() or callOn(); see Callbacks from other threads.

    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.



    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) — winscard on Windows, PCSC.framework on macOS, libpcsclite on 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 node-addon-api and cmake-js.