How to Ship Prebuilt Native Binaries in an npm Package

The most reliable way to ship native code in an npm package today is to build it in CI for each platform you support, target Node-API so one binary works across Node.js versions, and deliver the binaries either inside the package (bundled prebuilds) or as small per-platform packages listed in optionalDependencies with os and cpu fields. Both avoid asking users to compile with node-gyp at install time, which needs Python and a C/C++ toolchain on their machine and is one of the most common sources of failed installs. Whichever route you pick, keep a clear fallback and test installs on every platform before you publish.

Why compiling on install hurts users

A package that compiles during npm install pushes your build environment onto everyone downstream. Each user needs a compatible compiler, Python and headers for their Node.js version. Minimal containers, locked-down corporate laptops and Windows machines without build tools often lack them. Install scripts also run arbitrary code at install time, which is why some teams install with scripts disabled. Our page on npm install scripts and their risks explains that side of the problem.

Prebuilt binaries move the compiling to your CI, where you control the toolchain, and turn installation back into a download.

Start with Node-API

Older native addons were compiled against a specific Node.js ABI, so every major Node.js release needed new binaries. Node-API, the stable C interface maintained by the Node.js project, avoids that: an addon built against a given Node-API version keeps working on later Node.js releases that support it. For a package author that cuts the build matrix from platforms times Node versions down to roughly platforms alone. If you write the addon in Rust, the napi-rs project wraps Node-API and includes release tooling; for C++, the node-addon-api headers do the same job.

Three delivery patterns

PatternHow it worksStrengthsWatch out for
Download at installAn install script fetches a binary for the current platform from a release host, falling back to compilingSmall package; long-established tooling such as prebuild-installNeeds install scripts and network access to the host; breaks with scripts disabled or behind strict proxies
Bundled prebuildsBinaries for every platform ship inside the tarball and a loader picks one at runtimeNo install script, works offlinePackage size grows with every platform you add
Per-platform packagesA root package lists one package per platform in optionalDependencies; each declares os and cpu, so the package manager installs only the matchNo install script, each user downloads one binaryMore packages to publish in lockstep; lockfile and platform edge cases

The per-platform pattern is used by several widely installed build tools and is the default in napi-rs. A platform package's package.json is short:

{
  "name": "@example/widget-linux-x64-gnu",
  "version": "2.3.0",
  "os": ["linux"],
  "cpu": ["x64"],
  "main": "widget.linux-x64-gnu.node",
  "files": ["widget.linux-x64-gnu.node"]
}

The root package lists each platform package in optionalDependencies at the exact same version, and its loader tries to require the matching one, with a helpful error if none is installed. Some package managers also honour a libc field, which helps separate glibc and musl Linux builds.

A pre-publish checklist for native packages

  1. Decide the support matrix in writing. List operating systems, CPU architectures and, for Linux, glibc or musl. Put it in the README, and declare the Node.js range in the engines field.
  2. Build every target in CI. Use native runners where you can and cross-compile only where you have tested it. Our CI matrix calculator helps estimate the job count.
  3. Publish platform packages first, root package last. If the root goes out first, users can install a version whose binaries do not exist yet.
  4. Keep versions in lockstep. The root should pin exact versions of its platform packages, not ranges.
  5. Test from the tarball. Pack each package and install it into a clean project on each platform, as in how to test your package tarball before publishing.
  6. Test from a lockfile made elsewhere. Create a lockfile on one operating system and install from it on another. Some package-manager versions have mishandled platform-specific optional packages in this case, and you want to find that before your users do.
  7. Write a clear failure message. When no binary matches, say which platform was detected, which ones are supported and what the user can do next.
  8. Add provenance. Native binaries are hard for users to inspect, so publishing with provenance from CI is especially worthwhile.

When a fallback makes sense

Some packages ship a WebAssembly build as a slower but portable fallback for platforms without a native binary. Others keep a compile-from-source path for unusual systems, documented rather than automatic. A fallback is good for reach, but make sure it is visible: a silent switch to a slower path can look like a performance bug to users.

Frequently asked questions

Do I still need node-gyp if I ship prebuilt binaries?

Not for users on supported platforms. You may still use node-gyp or another build system in your own CI to produce the binaries, and some packages keep it as an optional fallback.

Why use Node-API instead of the older native addon approach?

Node-API is a stable interface, so one binary built against it keeps working across later Node.js releases that support that Node-API version. That greatly reduces the number of builds you publish.

What happens if a user is on a platform I do not support?

With per-platform optional packages, none will install and your loader should raise a clear error. Document the supported matrix and, if you can, offer a portable fallback.

For the rest of the release routine, see the npm pre-publish checklist or return to the Learn hub.