React Native Dotenv - Using Environment Variables in React Native
Overview
Managing environment variables is crucial in any React Native application, as it allows developers to configure different settings for development, staging, and production environments. React Native Dotenv is a handy library that simplifies the process of working with environment variables by loading them from a .env file. In this article, we will explore how to use React Native Dotenv in your React Native projects.
Installation
To start using React Native Dotenv in your project, you need to install the package from npm. Open your terminal and navigate to your project directory. Run the following command to install the library:
Build an AI-First Career, Master the Complete Skillset
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
AI Forward Deployed Engineer Program
Full-stack engineering, production AI and client-facing consulting
+1000 moreUsage:
a. .babelrc:
To enable the use of environment variables in your JavaScript code, you need to configure the .babelrc file. Add the following plugins to your .babelrc file:
b. .env:
Create a .env file in the root directory of your project. This file will contain your environment-specific variables. For example:
c. In users.js:
Now you can access your environment variables in your JavaScript code. For example, in a file called users.js, you can import the variables as follows:
[DEPRECATED] White and Black Lists
React Native Dotenv is used to support whitelist and blacklist options to filter which environment variables are exposed. However, these options have been deprecated in favor of a safer approach.
How Scaler Transformed Careers in Different Fields
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Allow and Block Lists
Instead of using whitelist and blacklist, React Native Dotenv now supports allow and block lists. With the allow option, you can explicitly specify the variables that are allowed to be accessed in your code. Conversely, the block option allows you to block specific variables from being accessed. Reference link
Safe Mode
By default, React Native Dotenv throws an error if a referenced environment variable is not defined in the .env file. You can enable safe mode to suppress these errors by setting the "safe" option to true in the .babelrc file.
Allow Undefined
By default, React Native Dotenv expects all environment variables to be defined in the .env file. However, if you want to allow undefined values for certain variables, you can set the allowUndefined option to true. This can be useful in situations where a variable may not always be required.
Turn Learning into Career Growth
Override envName
In some cases, you may need to use a different environment file based on specific conditions. React Native Dotenv allows you to override the default .env file by specifying the envName option in the .babelrc file. This allows you to load environment variables from a different file when needed.
Multi-env
React Native Dotenv supports multiple environment files. Instead of using a single .env file, you can create separate files for different environments, such as .env.development, .env.staging, and .env.production. The appropriate file will be loaded based on the current build environment, providing flexibility for configuring different environments.
TypeScript
React Native Dotenv works seamlessly with TypeScript. You can import the environment variables in your TypeScript code using the import {} from 'react-native-dotenv'; syntax.
Caching
React Native Dotenv incorporates caching to improve performance. During the build process, the loaded environment variables are cached to avoid reloading them unnecessarily. If any changes are made to the .env file, you need to restart the bundler or rebuild the app to apply the updated variables.
Conclusion
- React Native Dotenv simplifies the management of environment variables in React Native applications.
- It allows you to load environment variables from a .env file during the build process.
- You can configure the .babelrc file to enable the use of environment variables in your JavaScript code.
- React Native Dotenv supports multi-environment setups, TypeScript, and various configuration options.
- It provides a convenient and organized way to handle environment-specific settings.
- By centralizing environment variables, React Native Dotenv enhances code maintainability and improves developer productivity.