
Dynamically require and execute modules at runtime in React Native (Metro Bundler).
metro-requirex
enables dynamic module resolution and execution in React Native using Metro Bundler. It provides a safe, efficient, and Metro-compatible way to dynamically require()
modules and evaluate JavaScript code at runtime.
β
Dynamic Requires β Load Metro-bundled modules at runtime.
β
Eval Support β Execute JavaScript snippets with built-in requirex()
.
β
Works with React Components β Load and render dynamic React Native components.
β
No Metro Modifications β Uses require.resolveWeak()
and Metro's internal loader.
β
Safe & Performant β Isolated execution with new Function()
.
yarn add metro-requirex
or
npm install metro-requirex
Use requirex()
to dynamically load Metro-bundled modules.
import {requirex} from 'metro-requirex';
const lodash = requirex('lodash');
console.log(lodash.camelCase('hello world')); // "helloWorld"
Use evalx()
to execute JavaScript dynamically, supporting module imports.
import {evalx} from 'metro-requirex';
const code = `
const _ = require("lodash");
module.exports = _.kebabCase("React Native Rocks!");
`;
console.log(evalx(code)); // "react-native-rocks"
Since JSX is already transformed, you can evaluate and render React Native components dynamically.
import {evalx} from 'metro-requirex';
import {View, Text} from 'react-native';
const componentCode = `
module.exports = () => {
return React.createElement("Text", null, "Hello from a dynamic component!");
};
`;
const DynamicComponent = evalx(componentCode);
export default function App() {
return (
<View>
<DynamicComponent />
</View>
);
}
Dynamically loads a module in Metro.
moduleName
(string): The name of the module to require.
- The moduleβs exports if found.
null
if the module does not exist.
const moment = requirex('moment');
console.log(moment().format('YYYY-MM-DD'));
Executes JavaScript dynamically, supporting module imports.
code
(string): The JavaScript code to execute.
- The value of
module.exports
in the executed code.
const result = evalx(`
module.exports = "Dynamic Execution!";
`);
console.log(result); // "Dynamic Execution!"
Metro Bundler assigns opaque numeric IDs to modules at build time, meaning require('module')
doesnβt work dynamically.
Instead, requirex()
:
- Calls
require.resolveWeak(moduleName)
to retrieve Metroβs internal module ID. - Calls Metroβs internal
__r(moduleId)
to fetch the module dynamically.
- Uses
new Function()
to create a sandboxed execution scope. - Injects
requirex()
so that evaluated code can import modules dynamically. - Returns
module.exports
, mimicking a CommonJS module system.
Run the test suite to verify functionality:
yarn test
Contributions are welcome! To get started:
-
Clone the repo:
git clone https://github.com/crherman7/metro-requirex.git
-
Install dependencies:
yarn install
-
Make your changes and submit a pull request.
Licensed under the MIT License.
If you need dynamic module loading or evaluating JavaScript dynamically in React Native, metro-requirex
makes it fast, safe, and easyβwithout needing Metro modifications.
π Ready to supercharge your React Native app? Install metro-requirex
today! π