DEV Community

How to use .env file in a React Native Application

Dallington Asingwire on December 08, 2021

Using environment file (.env) to configure most critical variables like database credentials, api keys, is the most common practice in software dev...
Collapse
 
codingalgorithm profile image
Taofeek Ibrahim Opeyemi

Thank you.. This was helpful.

Collapse
 
samoht profile image
Thomas Lincoln • Edited

You are a hero man, this literally save my project. I want one day to be able to make posts this useful

Collapse
 
wilmela profile image
Wilmela

Thank you. This was helpful.

Collapse
 
dallington256 profile image
Dallington Asingwire

@wilmela , Glad to hear that!

Collapse
 
k3nnet profile image
k3nnet

Danko . this was helpful.

Collapse
 
nguyenquocvuonggit profile image
Nguyễn Quốc Vương

Thanks u. U are my hero in my heart

Collapse
 
mkhuzo profile image
mkhuzo zulu

worked perfectly

Collapse
 
pierre profile image
Pierre-Henry Soria ✨ • Edited

Great one Dallington 🙂
Any reason why you didn't install react-native-dotenv as devDependencies...? I'd recommend to do so.

Collapse
 
sandeep_singhmehra_12887 profile image
Sandeep Singh Mehra

Thank you

Collapse
 
marcoottina profile image
MarcoOttina

What to do if the '@env' is not found despite following each steps?

Collapse
 
dallington256 profile image
Dallington Asingwire

@marcoottina, do you get this error when using typescript?

Collapse
 
abdulmegadraws profile image
abdul-megadraws

Worked fine! But error in typescript import: Cannot find module '@env' or its corresponding type

Collapse
 
dallington256 profile image
Dallington Asingwire • Edited

@abdulmegadraws, to resolve error in typescript import: Cannot find module '@env' or its corresponding type, create env.d.ts file in your project root directory and define module '@env' for example;

declare module '@env' {
export const API_URL: string;
export const MAJOR_VERSION: string;
export const MINOR_VERSION: string;
export const PATCH_VERSION: string;
}