Environment variables are a staple in every project. Whether it's your API key, app name, or feature flags β you've probably used .env
files with react-native-config
or similar solutions.
But hereβs the catch: those tools are mostly bridge-based, rely on runtime JSON, and arenβt exactly the fastest β especially for performance-sensitive apps.
Thatβs why I built react-native-config-jsi
β a lightweight, ultra-fast JSI-based library that lets you access .env
variables natively, via C++.
β‘ Why this matters
- β
Synchronous access to
.env
variables at runtime (no async/Promise) - βοΈ Backed by C++ and integrated via React Native JSI
- π§ Minimal overhead β no bridges, no JSON, no wrappers
- π§© Works like
react-native-config
but faster and cleaner
π¦ Install
npm install react-native-config-jsi
π§ Setup
- Create your
.env
file:
API_KEY=your_api_key
APP_NAME=MyAwesomeApp
- For iOS:
- Run
pod install
inside theios/
directory - Add a Run Script Phase in Xcode Build Phases:
bash "${SRCROOT}/../node_modules/react-native-config-jsi/src/scripts/generate.sh"
This step ensures that your .env
gets compiled into a native source file during build time.
π Usage
import { RNConfig } from "react-native-config-jsi";
const apiKey = RNConfig.get("API_KEY");
console.log("API_KEY:", apiKey);
β οΈ After changing
.env
, make sure to rebuild or restart the app to apply changes.
π§© Behind the scenes
This library uses a small C++ layer to parse the .env
file at build time and expose the key-value pairs via a JSI binding. That means:
- No bridge involved
- No runtime parsing
- Instant reads from C++ memory
π Use cases
- Reading API keys or environment-specific configs at startup
- Feature toggles via env vars
- Optimizing config reads in performance-sensitive screens
π Links
- GitHub: pioner92/react-native-config-jsi
- NPM: react-native-config-jsi
This is especially useful for those who already use JSI-based tooling or want the cleanest possible access to .env
variables without touching the bridge.
Try it out and let me know what you think β happy to answer questions and improve it based on real-world feedback!
π§
Top comments (0)