Registry / devops / bindings

bindings

JSON →
library0.1.0jsnpmunverified

The `bindings` module is a robust helper library for authors of Node.js native addon modules, streamlining the process of locating and `require()`ing `.node` files. It abstracts away the complexities arising from different build tools (like `gyp`, `waf`, `node-pre-gyp`, and `node-qbs`) and build configurations (e.g., `Release` vs. `Debug`), which can place `.node` files in various directories. The library intelligently searches all common possible locations for the native module, ensuring the correct one is loaded for the current environment. The current stable version is 1.5.0. Releases are infrequent but target specific issues like Yarn PnP support or new build system integrations, reflecting a maintenance-driven cadence. Its primary differentiator is its exhaustive search logic and highly informative error output when a binding cannot be found.

npm install bindings
INSTALL
IMPORT
SIG · BINDINGS
B
bindings
devopsjavascriptv0.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

bindings
const bindings = require('bindings');
import bindings from 'bindings';
Primarily used in CommonJS environments due to its nature with native modules; direct ESM import might require bundler configuration or dynamic import for typical usage.
loadBindings
const myModule = bindings('my_native_addon.node');
const myModule = require('bindings')();
The `bindings` function must be called with the filename of the native addon you wish to load, typically found in the root of your native module project.
bindingsErrorDetails
try { const myModule = bindings('my_native_addon.node'); } catch (err) { console.error('Failed to load:', err.message, err.tries); }
When a native module cannot be found, `bindings` throws an Error object that includes an `err.tries` array detailing all paths it attempted to load, which is useful for debugging build issues.

Demonstrates how to use `bindings` to load a native `.node` file and illustrates its helpful error reporting when a binding is not found. Replace `nonExistentBinding.node` with your actual native module filename.

const bindings = require('bindings'); // Assuming your native module is named 'binding.node' and is built correctly. // In a real scenario, 'binding.node' would be replaced with your actual native module's filename. // For demonstration, we'll simulate a missing binding, which is a common use case for its error reporting. try { // This would ideally load your native addon and return its exports // For this example, we're calling a non-existent binding name to demonstrate the error handling const myBindings = bindings('nonExistentBinding.node'); console.log('Bindings loaded successfully (this should not happen in this example):', myBindings); // myBindings.your_c_function(); // Example usage of a native function } catch (error) { console.error('Failed to load native bindings:'); console.error(' Error message:', error.message); if (error.tries) { console.error(' Attempted paths:'); error.tries.forEach(path => console.error(` - ${path}`)); } else { console.error(' No specific paths recorded for this error type.'); } }
Debug
Known issues
gotchaThe `bindings` module relies on the native addon being correctly compiled for the target environment (Node.js version, OS, architecture). A common issue is a failed or incomplete native module build, leading `bindings` to report that it cannot find the `.node` file.
fix
Ensure your native module compiles successfully using `node-gyp rebuild` or your chosen build tool. Verify the `.node` file exists in one of the expected build output directories (e.g., `build/Release`, `build/Debug`, `compiled/<node-version>/<os>/<arch>`).
affects: >=1.0.0
gotchaThe search for the `.node` file originates from the first directory found containing a `package.json` file. If your project structure is unusual or you're running from a deeply nested script, this origin point might not be what `bindings` expects, causing it to look in the wrong relative paths.
fix
Structure your project such that the `package.json` for the native module is in a conventional location relative to its compiled `.node` files. If absolutely necessary, you might need to manually specify paths or use environment variables to influence the build process to place the `.node` file where `bindings` expects it.
affects: >=1.0.0
gotchaWhile `bindings` supports various build tools, switching between them (e.g., from `node-gyp` to `node-pre-gyp`) or different Node.js versions often requires a clean rebuild of the native module to prevent conflicts or out-of-date binaries.
fix
Always run `npm rebuild` or explicitly clean and rebuild your native module when changing Node.js versions, operating systems, or build configurations to ensure compatibility.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Could not load the bindings file. Tried: ...
The native `.node` addon file was not found in any of the common build output locations that `bindings` searches.
fix
Verify that your native module successfully compiled. Check the `err.tries` array in the error message to see where `bindings` looked, and ensure your `.node` file is present in one of those paths after compilation.
Error: The specified module could not be found. (for Windows)
Often indicates that a dependent DLL for the native `.node` addon is missing on Windows, rather than the `.node` file itself.
fix
Ensure all C/C++ runtime dependencies (e.g., Visual C++ Redistributables) are installed on the target Windows system. Check if your native addon has external DLL dependencies that are not in the system PATH or alongside the `.node` file.
Upgrade
Version history
0.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
Resources
bindings — npm install bindings · libregistry