react-native-hardwired
raw JSON → 0.10.2 verified Fri May 01 auth: no javascript
react-native-hardwired v0.10.2 enables runtime dynamic imports of pre-compiled React Native modules, bridging the gap between static bundling and deferred loading. Unlike standard React Native's static require or Metro's lazy loading, this library works with modules already transpiled by Babel/TypeScript, allowing you to load code on demand without ejection or native changes. It ships TypeScript definitions and is released on npm with monthly cadence. Key differentiator: no native modules, works with Hermes, and integrates directly with Metro's require system.
Common errors
error TypeError: (0 , _reactNativeHardwired.Hardwired) is not a constructor ↓
cause Using default import instead of named import for Hardwired.
fix
Change 'import Hardwired from ...' to 'import { Hardwired } from ...'
error Cannot find module './dyn/myModule' ↓
cause baseDir is incorrect or module file doesn't exist at the resolved path.
fix
Verify baseDir and module name. Ensure the module file exists and is precompiled.
error Module 'myModule' has no exports ↓
cause The dynamic module was not properly transpiled; missing module.exports.
fix
Add a build step that outputs CommonJS with module.exports.
Warnings
gotcha Modules must be precompiled with Babel/TypeScript before bundling. The library does not compile modules for you. ↓
fix Set up a build step (e.g., Babel) to transpile your dynamic modules into CommonJS before bundling with Metro.
gotcha The baseDir must be a relative path from the Metro bundle's root. Absolute paths are not supported. ↓
fix Use paths like './modules' or '../src/dynamic' that resolve relative to the project root.
breaking In v0.10.0, the constructor signature changed from (config) to (config: HardwiredConfig). The 'baseDir' property is now required. ↓
fix Update to new constructor with explicit baseDir: const loader = new Hardwired({ baseDir: './dyn' });
deprecated The 'load' method's callback parameter is deprecated since v0.9.0. Only promise-based usage is supported. ↓
fix Use async/await or .then() instead of passing a callback to loader.load().
Install
npm install react-native-hardwired yarn add react-native-hardwired pnpm add react-native-hardwired Imports
- Hardwired wrong
import Hardwired from 'react-native-hardwired'correctimport { Hardwired } from 'react-native-hardwired' - useHardwired wrong
import { useHardwired as useHardwired } from 'react-native-hardwired'correctimport { useHardwired } from 'react-native-hardwired' - type HardwiredModule wrong
import { HardwiredModule } from 'react-native-hardwired'correctimport type { HardwiredModule } from 'react-native-hardwired'
Quickstart
import { Hardwired } from 'react-native-hardwired';
// Create a hardwired loader instance
const loader = new Hardwired({ baseDir: './modules' });
// Dynamically load a precompiled module at runtime
async function loadModule() {
try {
const module = await loader.load('my-dynamic-module');
module.default(); // call default export
} catch (error) {
console.error('Failed to load module:', error);
}
}
loadModule();