Node.js Core Modules for React Native
This package, `node-libs-react-native`, provides React Native compatible implementations of various Node.js core modules such as `stream`, `http`, `buffer`, and `process`. It serves as a fork of `node-libs-browser`, specifically adapted for the React Native environment by swapping certain underlying packages for better mobile compatibility. The primary mechanism involves exposing a mapping of Node module names to their absolute React Native compatible file paths. This mapping is then leveraged within React Native Packager's `metro.config.js` resolver or Webpack's `resolve.alias` configuration to correctly resolve module imports within a React Native project. Additionally, it offers a dedicated `globals` module designed to shim Node.js specific global variables like `Buffer` and `process` into the React Native runtime. The current stable version, 1.2.1, was last published over five years ago, indicating that the project is likely abandoned. Given the rapid evolution of both Node.js and React Native, its utility in modern projects is limited, and more actively maintained alternatives or specific polyfills are generally recommended.
Common errors
-
Error: Unable to resolve module `stream` from `...`. Module does not exist in the Haste module map.
cause A React Native module is trying to import a Node.js core module (e.g., `stream`, `http`, `crypto`) that Metro is not configured to resolve.fixEnsure your `metro.config.js` file is correctly set up with `resolver.extraNodeModules: require('node-libs-react-native')`. Restart your Metro bundler after making changes to the config. -
ReferenceError: Buffer is not defined
cause Your React Native application or one of its dependencies is attempting to use the `Buffer` global object, which is a Node.js specific global, without it being polyfilled.fixAdd `require('node-libs-react-native/globals');` to the very top of your application's entry file (e.g., `index.js`). This shims the `Buffer` and `process` globals into the React Native environment.
Warnings
- breaking The package is largely unmaintained, with the last update over five years ago (version 1.2.1). It may not be compatible with recent versions of React Native or Node.js, leading to build failures or runtime errors.
- gotcha Shimming Node.js core modules in a non-Node environment like React Native can introduce subtle behavioral differences or incomplete implementations. Not all Node.js APIs or module features are fully replicated.
- gotcha The package does not automatically provide Node.js global variables like `Buffer` or `process`. These must be explicitly shimmed by importing `node-libs-react-native/globals` early in your application's entry point.
- gotcha Some Node.js core modules, such as `fs` (file system), `child_process`, `cluster`, `dgram`, `module`, `net`, `readline`, `repl`, and `tls`, are not provided or are explicitly marked as unsupported/mocked with minimal functionality in this package. Relying on them will fail at runtime.
Install
-
npm install node-libs-react-native -
yarn add node-libs-react-native -
pnpm add node-libs-react-native
Imports
- extraNodeModules
import extraNodeModules from 'node-libs-react-native';
const extraNodeModules = require('node-libs-react-native'); - globals shim
import 'node-libs-react-native/globals';
require('node-libs-react-native/globals');
Quickstart
// metro.config.js
module.exports = {
resolver: {
// This configuration instructs Metro to resolve Node.js core modules
// using the React Native compatible implementations provided by the package.
extraNodeModules: require('node-libs-react-native'),
},
// It's also common to need a custom assetExts array
// if you're importing assets not natively supported by React Native,
// though not directly related to node-libs-react-native.
// transformer: {
// getTransformOptions: async () => ({
// transform: {
// experimentalImportSupport: false,
// inlineRequires: true,
// },
// }),
// },
};