Full answer
What an addon is
- A shared library with a .node extension that registers functions and values for JavaScript. require() loads it with process.dlopen.
- It can call operating system APIs, use hardware, link existing C or C++ libraries (image codecs, databases, crypto) and run CPU-heavy code at native speed.
N-API
- Node-API (napi) is a C API with a stable ABI. Addons built against a given N-API version keep working on newer Node versions without recompiling. The older NAN and direct V8 APIs break often.
- Build tools: node-gyp (needs Python and a C++ compiler), cmake-js, or Rust with napi-rs, and C++ with node-addon-api (a header wrapper). Many packages ship prebuilt binaries (prebuildify, prebuild-install), so users do not need a compiler.
- Example packages that use addons: sharp, bcrypt, better-sqlite3, and many database drivers.
Costs and risks
- A bug can crash the whole process (segmentation fault), not just throw an error.
- Platform and architecture builds, glibc versus musl (Alpine), and install failures in CI.
- Calls across the JavaScript and native boundary are not free, so batch work.
- Long native calls block the event loop unless you use async work (napi_create_async_work) or a worker thread.
Alternatives
- WebAssembly: runs in V8 in a sandbox, same file on every platform, no compiler on the server, good for compute (codecs, parsers). No direct system access, and memory is a linear buffer.
- worker_threads: when the heavy code is JavaScript.
- A separate process or service (child_process, gRPC) for strong isolation.
- Existing packages. Check for a prebuilt addon before writing one.
Short example of an addon function in C++ with node-addon-api (shown as text, not compiled here): a function Add(info) reads two numbers from info, adds them and returns Napi::Number, and the module exports it with exports.Set("add", ...).
const { createRequire } = require('node:module');
// What the runtime offers for native code
console.log('N-API version is reported:', typeof process.versions.napi === 'string');
console.log('require handles these extensions:', Object.keys(require.extensions).join(' '));
// Loading something that is not a real addon fails with a clear error code.
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const fake = path.join(fs.mkdtempSync(path.join(os.tmpdir(), 'addon-')), 'fake.node');
fs.writeFileSync(fake, 'this is not a shared library');
try { createRequire(__filename)(fake); } catch (err) { console.log('loading a broken .node file ->', err.code); }
fs.rmSync(path.dirname(fake), { recursive: true });
// WebAssembly: a tiny module (hand-written bytes) that exports add(a, b).
const bytes = new Uint8Array([
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, // header
0x01, 0x07, 0x01, 0x60, 0x02, 0x7f, 0x7f, 0x01, 0x7f, // type: (i32, i32) -> i32
0x03, 0x02, 0x01, 0x00, // one function
0x07, 0x07, 0x01, 0x03, 0x61, 0x64, 0x64, 0x00, 0x00, // export "add"
0x0a, 0x09, 0x01, 0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6a, 0x0b, // body: local.get 0, local.get 1, i32.add
]);
const instance = new WebAssembly.Instance(new WebAssembly.Module(bytes));
console.log('WebAssembly add(2, 3) =', instance.exports.add(2, 3));
console.log('WebAssembly add(2147483647, 1) wraps to', instance.exports.add(2147483647, 1));The script shows what Node reports about N-API, what happens when a broken addon is loaded, and a very small WebAssembly module written by hand. No C++ compiler is used here. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): N-API version is reported: true require handles these extensions: .js .json .node loading a broken .node file -> ERR_DLOPEN_FAILED WebAssembly add(2, 3) = 5 WebAssembly add(2147483647, 1) wraps to -2147483648
Key points
- An addon is a .node shared library loaded by require.
- N-API has a stable ABI across Node versions.
- Prebuilt binaries remove the compiler requirement for users.
- A native crash takes down the whole process.
- WebAssembly and workers are common alternatives.
Common mistakes
- Writing an addon for work that a JavaScript library already does well.
- Targeting the V8 API directly and breaking on every Node upgrade.
- Shipping an addon without prebuilt binaries to musl or ARM users.
- Running a long native call on the main thread.
- Forgetting that a native crash cannot be caught with try/catch.
Follow-ups
- What is the difference between N-API and node-addon-api?
- How do prebuilt binaries get chosen at install time?
- How would you call a native function without blocking the event loop?
Tips
- Define addon and N-API in two sentences.
- List the risks, not only the benefits.
- Offer WebAssembly and workers as alternatives.