napi-macros is a lightweight, header-only C/C++ library providing a set of utility macros designed to simplify the development of Node.js N-API modules. It aims to reduce boilerplate code commonly associated with N-API, particularly for argument parsing, return value handling, and function/constant exports. As of April 2026, the current stable version is 2.2.2. This package is maintained by mafintosh, a prominent figure in the Node.js ecosystem, and typically sees updates in response to N-API specification changes or user contributions, rather than a fixed release cadence. Its key differentiator is its direct, macro-based approach, offering a minimal abstraction layer over the raw N-API C functions, making it suitable for developers who prefer fine-grained control and low-level C/C++ integration compared to higher-level C++ wrappers like `node-addon-api` or Rust-based solutions such as `napi-rs`.
npm install napi-macrosVerified import paths — ran on the pinned version, not inferred.
This C++ snippet demonstrates a basic N-API module using `napi-macros`. It defines a `times_two` method that takes a single integer argument from JavaScript, multiplies it by two, and returns the result. It showcases `NAPI_METHOD`, argument parsing (`NAPI_ARGV`, `NAPI_ARGV_INT32`), returning values (`NAPI_RETURN_INT32`), and module initialization/export (`NAPI_INIT`, `NAPI_EXPORT_FUNCTION`).
Thoroughly validate JavaScript input types on the Node.js side before calling native methods, or implement explicit runtime checks within the C++ code to handle unexpected types gracefully using N-API's status checks.
Ensure that development and build environments are fully equipped with the necessary toolchain for `node-gyp`. Consider pre-compiling binaries for different platforms and architectures for distribution, or explore alternatives like `napi-rs` which streamline native addon distribution with prebuilt binaries.
Familiarize yourself with the macro expansions to understand the underlying N-API calls. Use a text editor or IDE capable of macro expansion previews if available. Develop thorough unit tests for native modules to catch issues early.
Ensure your Node.js version and build environment are consistent. If encountering this, check for conflicting header inclusions or global macro definitions. Upgrading to the latest stable Node.js and `napi-macros` versions might resolve such conflicts.
Add or correct the `include_dirs` entry in your `binding.gyp` file to include `napi-macros` by dynamically resolving its path: `'include_dirs': [ "<!(node -e \"require('napi-macros')\")" ]`.Review the N-API documentation for the specific function causing the error (`napi_create_threadsafe_function` in this case) and ensure all arguments conform to its requirements. The `env` parameter (of type `napi_env`) is crucial and usually obtained from the `napi_callback_info` argument within a `NAPI_METHOD`.
Ensure that NAPI argument parsing macros like `NAPI_ARGV` are used within a `NAPI_METHOD` block, which automatically provides `env` and `info` in the correct scope, or that your custom N-API function explicitly defines `napi_env env, napi_callback_info info` as its parameters.
No dependency data recorded yet.