React Native Node is a powerful library that enables running a full Node.js process as a background service within a React Native Android application. It provides a unique solution for developers who need to leverage Node.js capabilities directly in their mobile apps.
- Full Node.js Integration: Run a real Node.js process behind a React Native app, allowing access to the full Node.js ecosystem
- Background Processing: Offload heavy computational tasks from the main JavaScript thread
- Android Support: Enables running Node.js v7.1.0 with V8 on Android devices
- Flexible Use Cases:
- Run HTTP servers
- Use Node streams
- Interact with the filesystem
- Perform background processing
Ideal for applications that require:
- Backend services within a mobile app
- Complex data processing
- Filesystem interactions
- Networking operations that benefit from Node.js capabilities
- Deprecation Notice: The library is no longer actively maintained
- Platform Support: Currently supports Android only
- Recommended Alternative: Node.js Mobile by Janea Systems
The library includes a precompiled Node.js binary (v7.1.0) that runs as a separate process, allowing full Node.js functionality within a React Native mobile application.
- React Native version 0.47.0 or higher
- Node.js for development
- Android device or emulator (iOS not supported)
Install the package using npm:
npm install --save react-native-nodeLink the native module:
react-native link react-native-node-
Create a background Node.js project (e.g., in a
./backgrounddirectory) -
In your React Native app, import and use RNNode:
import RNNode from "react-native-node";
class MyApp extends Component {
componentDidMount() {
// Start the background Node.js process
RNNode.start();
// Optionally, start with custom arguments
RNNode.start(['--color', 'red', '--port', '3000'])
}
componentWillUnmount() {
// Stop the background process when no longer needed
RNNode.stop();
}
}- Bundle the background application:
./node_modules/.bin/react-native-node insert ./backgroundThis command does two things:
- Compresses your background app into
./android/app/src/main/res/raw/rnnodebundle - Updates
AndroidManifest.xmlto include a Service class
- Build and run the mobile app:
react-native run-android- To automate bundling before starting the app, add a
prestartscript inpackage.json:
"scripts": {
"prestart": "react-native-node insert ./background",
"start": "node node_modules/react-native/local-cli/cli.js start"
}- For debugging, use
adb logcatwith specific tags:
adb logcat *:S nodejs:V ReactNative:V ReactNativeJS:V- Currently supports Android only
- Relies on a prebuilt Node.js v7.1.0 binary
- Limited support for packages with native bindings
The RNNode module provides functionality to manage a Node.js process running alongside your React Native application.
Starts the Node.js process with optional command-line arguments.
- Parameters:
args(optional): An array of command-line arguments to pass to the Node.js process- If no arguments are provided, an empty array will be used
- Returns:
void
Example:
import RNNode from 'react-native-node';
// Start Node.js process without arguments
RNNode.start();
// Start Node.js process with custom arguments
RNNode.start(['--port', '3000']);Stops the running Node.js process.
- Parameters: None
- Returns:
void
Example:
import RNNode from 'react-native-node';
RNNode.stop();The library includes a CLI tool react-native-node which can be used for additional operations (configuration details available in the CLI tool itself).
The project is organized into several key directories and files:
bin.js: Core executable script for the projectindex.js: Main entry point for the modulepackage.json: Project configuration and dependency managementLICENSE: Project licensing informationCHANGELOG.md: Record of version changes and updates
The android/ directory contains platform-specific implementation for Android:
android/build.gradle: Gradle build configuration for Androidandroid/src/main/java/com/staltz/reactnativenode/: Contains Java classes for React Native Node moduleRNNodeModule.java: Module implementationRNNodePackage.java: Package configurationRNNodeService.java: Service-related functionalityRNNodeThread.java: Thread management for Node.js operations
The example/ directory provides a comprehensive demo application:
example/android/: Android-specific project configurationexample/background/: Background processing scriptsexample/index.android.js: Main Android entry pointexample/package.json: Example project dependencies
.eslintrc: ESLint configuration for code linting.flowconfig: Flow type checking configuration.gitignore: Git ignore rules.npmignore: NPM package publishing exclusion rules
screenshot.png: Visual preview or documentation image
- React Native: Mobile application framework (version 0.48.3)
- Node.js: Backend runtime environment
- JavaScript: Primary programming language
- Babel: JavaScript transpiler (babel-preset-react-native)
- ESLint: Code linting tool
- Gradle: Build automation tool for Android
- React: UI component library (version 16.0.0-alpha.12)
- Cheerio: HTML parsing and manipulation library
- Tar: File compression and extraction library
- Mkdirp: Directory creation utility
- Yargs: Command-line argument parsing
- Android SDK: Compilation SDK version 23
- Android Support Library: AppCompat v7
- Apache Commons Compress: Compression utilities
- npm/yarn: Package management
- React Native CLI: Command-line interface for React Native projects
When running Node.js in a mobile environment, be mindful of resource consumption. The bundled Node.js binary (v7.1.0) is precompiled and may have performance implications for complex applications.
Utilize Android logging to troubleshoot your Node.js background process. Key logging tags include:
RNNodeThread: Process start, termination, and error eventsRNNodeService: Binary preparation and tar/untar operationsRNNode: General library-specific logging
Debug command:
adb logcat *:S nodejs:V ReactNative:V ReactNativeJS:V
- Android: Fully supported
- iOS: Not supported due to Apple's restrictions on Just-In-Time compilation
- Native binding support is limited
- Most packages without explicit Android native bindings may work
- Recommended compilation approach: Use Termux on an Android device for native package compilation
The prebuilt Node.js binary is provided for convenience. For heightened security, consider compiling the binary yourself following the "NodeBase" approach.
This library was originally developed to support the Scuttlebutt ecosystem on mobile platforms, specifically for the mmmmm mobile app.
We welcome contributions to react-native-node! Here are some guidelines to help you get started:
- Fork the repository and create your branch from
master - Ensure your code follows the project's ESLint configuration
- Uses single quotes for strings
- Follows strict mode
- Maintains consistent code style
- Run all existing tests before submitting a pull request
- Include clear, concise descriptions with your pull requests
The project uses ESLint with the following key style rules:
- Single quotes for strings
- Camel case for variables and functions
- Semicolons are required
- Consistent spacing around operators
- No trailing whitespaces
- Use the GitHub Issues section to report bugs or suggest features
- Provide a clear and detailed description
- Include steps to reproduce the issue if applicable
- Specify the version of react-native-node you are using
If you'd like to support the project, you can do so via Patreon for the project author.
This project is licensed under the MIT License.
The MIT License is a permissive free software license originating at the Massachusetts Institute of Technology. It puts very limited restrictions on reuse and has, therefore, high license compatibility.
- Commercial use
- Modification
- Distribution
- Private use
- Liability
- Warranty
For the complete license text, please refer to the LICENSE file in the repository.
Copyright (c) 2017-present André Staltz (staltz.com)