React Native Dotenv - Using Environment Variables in React Native

Learn via video courses
Topics Covered

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:

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Usage:

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:

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

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.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

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

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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.