weak-node-api
raw JSON → 0.1.1 verified Sat Apr 25 auth: no javascript
A linkable and runtime-injectable Node-API implementation that allows native addons to be built without direct linking against a Node-API provider. Version 0.1.1 is the latest stable release, part of the React Native Node-API project. It is a thin shim that exposes only Node-API function declarations, enabling runtime injection of the actual implementation by the host (e.g., React Native runtime), solving Android dynamic linker symbol resolution issues. Unlike typical Node-API bindings, it decouples addon compilation from the runtime, supporting React Native and potentially other contexts. Dependencies: none. Release cadence: early stage, active development.
Common errors
error Cannot find module 'weak-node-api' or its corresponding type declarations. ↓
cause TypeScript cannot resolve types; likely missing @types or declaration files.
fix
Install the package and ensure tsconfig.json includes 'node' types. If still failing, add 'skipLibCheck': true.
error ERR_REQUIRE_ESM: require() of ES Module /node_modules/weak-node-api/index.mjs from <file>.js not supported. ↓
cause Using CommonJS require() on an ESM-only package.
fix
Switch to ES modules (use 'import') or use dynamic import: const weak = await import('weak-node-api');
error TypeError: WeakNodeApiHost is not a constructor. ↓
cause Using the old class name WeakNodeApiHost which was renamed in v0.1.0.
fix
Use NodeApiHost instead.
Warnings
breaking WeakNodeApiHost class renamed to NodeApiHost in v0.1.0. ↓
fix Replace WeakNodeApiHost with NodeApiHost.
gotcha All exports are ESM-only; there is no CommonJS build. Using require() will fail. ↓
fix Use import statements; if in CommonJS project, use dynamic import or switch to ESM.
gotcha TypeScript users must ensure 'resolveJsonModule' and 'esModuleInterop' are set for proper type support. ↓
fix Add these to tsconfig.json: { compilerOptions: { module: 'esnext', moduleResolution: 'bundler', esModuleInterop: true } }
deprecated The package is in early alpha; API may change without major semver bump. ↓
fix Pin exact version and monitor changes.
Install
npm install weak-node-api yarn add weak-node-api pnpm add weak-node-api Imports
- NodeApiHost wrong
import { WeakNodeApiHost } from 'weak-node-api'correctimport { NodeApiHost } from 'weak-node-api' - NapiMixin wrong
const NapiMixin = require('weak-node-api').NapiMixincorrectimport { NapiMixin } from 'weak-node-api' - ModuleFunctionMap
import type { ModuleFunctionMap } from 'weak-node-api' - defineNapiModule wrong
import defineNapiModule from 'weak-node-api'correctimport { defineNapiModule } from 'weak-node-api'
Quickstart
import { NodeApiHost, defineNapiModule, NapiMixin } from 'weak-node-api';
// Define a simple Node-API module
const myModule = defineNapiModule({
napiVersion: 8,
functions: {
add: (env, napiValue) => {
// implementation placeholder
return 42;
}
} as any
});
// Create a host that injects the implementation
const host = new NodeApiHost({
module: myModule,
// other options
});
// Mixin example
class MyAddon extends NapiMixin {
// NapiMixin provides Node-API context
}
console.log('weak-node-api initialized');