zig-build, currently at version 0.3.0, is a modern library designed for building and cross-compiling Node.js native addons, with a strong focus on Node-API (N-API). It differentiates itself from traditional tools like node-gyp and cmake-js by directly leveraging the Zig compiler and its Clang wrapper, eliminating the need for system compilers. This approach enables first-class cross-compilation, automatic build caching, and static linking of libc++. The library exclusively supports N-API addons, intentionally deprecating legacy NAN addons, and does not handle native addon downloading at install time. Instead, it promotes the use of npm's `optionalDependencies` combined with `os`/`cpu` specific packages for improved user experience and reduced runtime dependencies. `zig-build` functions as a library within a JavaScript or TypeScript build script, providing configuration flexibility. The release cadence is currently irregular, characteristic of a rapidly evolving project in its early stages.
npm install zig-buildVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to configure and build a native Node.js addon for multiple operating system and architecture targets (Windows x64, Linux x64, Linux ARM64) within a single `build` script call.
Rewrite legacy NAN addons to use the Node-API (N-API) specification, leveraging `node-addon-api` for C++ abstractions.
Structure your project to use separate `optionalDependencies` entries in `package.json` for different target platforms, each containing the appropriate prebuilt native addon. Do not expect `zig-build` to handle runtime compilation on end-user machines.
Create a dedicated build script (e.g., `build.mjs` or `build.ts`) and import the `build` function. Always explicitly define the `target` property within your build configuration objects for each desired platform.
Ensure `node-addon-api` is installed in your project by running `npm install --save-dev node-addon-api` or `yarn add --dev node-addon-api`.
Carefully review the console output for specific Zig compiler errors. Check your `sources` paths, `std` version, `defines`, `libraries`, and `target` options in your build configuration object. Ensure all source files exist and are correctly referenced.
Ensure your build script is an ESM module (e.g., has a `.mjs` extension or `"type": "module"` in `package.json`) and use `import { build } from 'zig-build'`. If using CommonJS, dynamic `import()` might be an option, but native ESM is recommended.Install `node-addon-api` as a development dependency: `npm install --save-dev node-addon-api` or `yarn add --dev node-addon-api`.
Consult the official Zig documentation or run `zig targets` in your terminal to see a list of supported target triples. Ensure your target string follows the correct format (e.g., `x86_64-linux-gnu`).