bun-ffi

Call native C and C++ shared libraries from JavaScript using Bun's FFI API.

Updated Jun 22, 2026
One-click install
npx skills add https://github.com/aicodepro/ai-agent-nexi --skill bun-ffi-aicodepro
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: bun-ffi
Source: https://github.com/aicodepro/ai-agent-nexi/tree/main/agent/skills/bun-ffi
Command: npx skills add https://github.com/aicodepro/ai-agent-nexi --skill bun-ffi-aicodepro

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Integrating existing native C/C++ libraries with JavaScript typically requires writing complex Node.js native addons with N-API or node-gyp. This Skill provides direct guidance for using Bun's built-in bun:ffi module to load shared libraries and call native functions without any compilation toolchain. ## Core Features & Use Cases - Library Loading: Load system or custom shared libraries (.dll, .so, .dylib) with cross-platform path resolution using dlopen and suffix. - Type Mapping & Function Definitions: Map C types (integers, floats, pointers, cstrings) to FFIType definitions and declare function signatures with arguments and return types. - Memory & Pointer Operations: Work with pointers, ArrayBuffers, structs, and C strings using ptr, CString, and toArrayBuffer. - Callbacks: Pass JavaScript functions as C callbacks for operations like custom sort comparators. - Use Case: Call SQLite's C API directly from a Bun script to open a database, execute SQL, and close the connection without any ORM or native addon. ## Quick Start Show me how to call a function from a native C shared library in Bun using bun:ffi with dlopen and FFIType definitions.

Frequently Asked Questions about bun-ffi

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I call a C function from JavaScript in Bun?

Use dlopen from bun:ffi to load the shared library and declare the function signature with FFIType argument and return types. Then invoke it via lib.symbols.functionName with matching arguments.

How do I pass strings to C functions using bun:ffi?

Convert the string to a null-terminated Buffer with Buffer.from("text\0") and pass it using ptr(). For strings returned from C, wrap the pointer in new CString(result) to convert it back to a JavaScript string.

Does bun:ffi work on Windows, Linux, and macOS?

Yes, bun:ffi is cross-platform. Use the exported suffix value to resolve the correct library extension: dll on Windows, so on Linux, and dylib on macOS, then build platform-specific paths as needed.

Why does my bun:ffi call cause a segmentation fault?

Segmentation faults usually come from invalid pointers, wrong FFIType declarations that mismatch the actual C signature, or accessing freed memory. Verify that argument and return types exactly match the C header definitions.

Can I use JavaScript callbacks with C libraries in Bun?

Yes, use the callback function from bun:ffi to wrap a JavaScript function with a C-compatible signature, then pass its ptr to the native function. Call close() on the callback when finished to release resources.