Dotenv
Intro
dotenv is a tool helps us to access variables in .env file in anywhere under the project.
In nodejs
installation
npm install dotenv --save
import and use it under app.js
require('dotenv').config()
console.log(process.env)
once the dotenv is imported under the most top file (app.js), no need to require it in other files. Directly use the syntax:
process.env.[variable.name]
to access the variable
in react
installatation
npm install react-dotenv
import and use
import React from "react";
import env from "react-dotenv";
export function MyComponent() {
return <div>{env.API_URL}</div>;
}