Node.js
Node.js runs your C++ two ways: as WebAssembly, one .wasm file for every operating system, or as a Node-API addon, native code built for each platform. Both keep the initNative() contract, so the same app code runs on either.
Which one
| WebAssembly | Node-API addon | |
|---|---|---|
| Output | one .wasm and a loader, for every OS and CPU | a .node file per platform, and a loader that picks one |
| Built on | any machine with Docker | Linux and Windows addons in Docker on any host, macOS addons on a Mac |
| Files | m.FS reads and writes the host disk | C++ uses the OS directly; there is no m.FS |
| Threads | -r mt runs pthreads, with no setup | one Node environment per process, so no worker_threads |
Take the addon for Electron's main process, for native memory or system access, or to use a port with nothing to build. It is not automatically faster: some calls are quicker as an addon and some on wasm (measured), so measure your workload before switching.
WebAssembly
Install crossbind, point paths.output at dist and build with -e node:
The loader, dist/my-app-wasm-wasm32-st-release.node.js, is CommonJS, so require and import both load it. Calls are synchronous: Node.js runs the module without a worker.
The same build writes dist/node/wasm.mjs, which exports every bound name directly: import { initNative, Native } from '../dist/node/wasm.mjs'.
Import headers directly
A Node.js app can also import its native code the way a bundler app does: its own header, a library header, a conan: header, a .rs file or a cargo: crate.
While developing, start Node through crossbind/node/dev: it builds before the app starts when the native sources, the config or the imports changed. The second line also restarts the app when a file in src/native changes:
crossbind build -e node finds these imports in your sources, binds them and writes the hooks that serve them, dist/node/wasm.register.mjs. A deployed app starts with those and needs no crossbind:
An import the build did not see, such as a specifier computed at run time, fails until you build again. require() of a header works too; read its names after initNative() resolves.
Files and threads
m.FS is the host filesystem: C++ opens real paths, relative to process.cwd(), and /memfs/ is in memory. Data files a library reads land in dist/data; ship them with the loader. Build with -r mt for a multithreaded module, which Node runs with no setup; dist/node/wasm.mjs covers the single-threaded build only.
Node-API addon
An addon is native code inside the Node process: a .node file per platform and architecture, and a CommonJS loader that picks the right one (on Linux, also by C library).
Prebuilt
Each port publishes its addon prebuilt as @crossbind/port-<name>-standalone-napi: no build, no Docker, no crossbind. npm installs only the addon for the machine. These packages need Node.js 22.12 or later.
Your own addon
Build one when the app has C++ of its own. Install the Node-API host next to crossbind, keep output: 'dist' in the config, and build this machine's platform with -p host:
That writes an addon per architecture (such as dist/my-app.darwin-arm64.node), the loader dist/my-app.native.cjs, dist/node/napi.mjs with every bound name, and library data files in dist/data. A port needs its desktop packages, such as @crossbind/port-zlib-linux, installed and listed in dependencies; see crossbind ports.
The import hooks work the same way: with @crossbind/core-embind-napi installed, crossbind/node/dev builds the addon instead of the wasm, and a deployed app starts with --import ./dist/node/napi.register.mjs.
| Platform | Built on | Runs on |
|---|---|---|
darwin | a Mac with Xcode's command line tools | macOS 11 or later |
linux | any host, in Docker | glibc 2.28 or later |
linuxmusl | any host, in Docker | musl 1.2.5 or later (Alpine 3.21 and newer) |
win32 | any host, in Docker | Windows 10 or later |
-p host picks this machine's platform (on Linux, linux or linuxmusl by its C library); -p darwin,linux,linuxmusl,win32 builds all four, each for arm64 and x64. A Mac builds its own addons without Docker. To publish addons so npm installs only the one a machine needs, see Publishing packages.
Limits
- One Node environment per process. The addon cannot load again in a
worker_threadsworker;initNative()there rejects with an error. - A Linux addon loads only on the C library it was built for. Ship both
linuxandlinuxmuslwhen the app may run on Alpine; the loader picks between them. - The browser build does not run in Node. A
-e browserloader is built for the web only; build with-e node.
Complete apps: backend-nodejs-wasm, backend-nodejs-native and backend-nodejs-standalone. Each is also a template: npm create crossbind@beta my-app Backend Node.js WebAssembly, with Node-API or Standalone as the last word.