Helper for Loading Node.js Native Addons

1.5.0 · active · verified Sun Apr 19

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.

Common errors

Warnings

Install

Imports

Quickstart

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.');
  }
}

view raw JSON →