Helper library for loading native Node.js bindings in node-rs projects.
npx @tessl/cli install tessl/npm-node-rs--helper@1.6.0@node-rs/helper is a TypeScript utility library that provides a robust solution for loading native Node.js bindings (.node files) in node-rs projects. It dynamically resolves and loads platform-specific native modules based on the current system architecture and platform, with comprehensive error reporting and support for multiple loading strategies.
npm install @node-rs/helperimport { loadBinding } from "@node-rs/helper";For CommonJS:
const { loadBinding } = require("@node-rs/helper");import { loadBinding } from "@node-rs/helper";
// Load binding from local directory (common case)
const nativeModule = loadBinding(__dirname, 'mymodule', '@my-package/core');
// Simple usage with just directory and filename
const simpleModule = loadBinding(__dirname, 'index');
// Use the loaded native module
nativeModule.someNativeFunction();@node-rs/helper implements a two-stage binding resolution strategy:
The library supports multiple platform architectures including Windows (x64, ia32, arm64), macOS (x64, arm64), Linux (x64, arm64, armv7), FreeBSD, Android, and other platforms.
Dynamically loads platform-specific native Node.js bindings with automatic platform detection and fallback strategies.
/**
* Load native binding file from dirname with platform-specific resolution
* @param dirname - Directory path where the .node binding file is located
* @param filename - Optional filename (defaults to 'index'), should match napi.name in package.json
* @param packageName - Optional package name (e.g., '@swc/core'), should match name in package.json
* @returns The loaded native module
* @throws Error with detailed message if no compatible binding can be loaded
*/
function loadBinding(dirname: string, filename?: string = 'index', packageName?: string): any;Parameters:
dirname: string - The directory where the native binding file is locatedfilename?: string = 'index' - The base filename for the native binding (without platform suffix and .node extension)packageName?: string - The package name used for node_modules resolution (e.g., '@swc/core')Returns:
any - The loaded native module object with its exported functions and propertiesLoading Strategy:
packageName is provided, attempts to load ${packageName}-${platformArchABI} from node_modules${filename}.${platformArchABI}.node from the specified directoryError Handling: Throws a detailed Error if loading fails, including:
Platform Support:
Usage Examples:
import { loadBinding } from "@node-rs/helper";
// Load with package name for node_modules resolution
try {
const swcModule = loadBinding(__dirname, 'swc', '@swc/core');
console.log('SWC native module loaded successfully');
} catch (error) {
console.error('Failed to load SWC:', error.message);
}
// Load from local file only
try {
const localModule = loadBinding(__dirname, 'my-native', undefined);
const result = localModule.processData('input');
} catch (error) {
console.error('Failed to load local binding:', error.message);
}
// Typical usage in a package's main entry point
module.exports = loadBinding(__dirname, 'package-name', '@my-org/package-name');The loadBinding function provides comprehensive error reporting for common failure scenarios:
Missing Platform-Specific Packages:
Can not load bindings
Installed packages: [package-name-win32-x64, package-name-linux-x64]File Exists But Load Failed:
Can not load bindings, file: /path/to/binding.linux-x64.node existed but error occurred while require it: Error messageNo Compatible Bindings Found:
Can not load bindings