If you want to use.env file in your node js project. Hear all stapes.
npm install dotenv.
2: Stape creates .env file in the root directory.
3: Stape import dotenv in the main file at the top of the line.
import dotenv from "dotenv"
4: Stape after importing all package config the dotenv like that :
dotenv.config({
path: './env'
});
It's the path of the env file path: './env'
5: Stape Added dotenv as an experimental in package.json in the script.
"scripts": {
"dev": "nodemon -r dotenv/config --experimental-json-modules src/index.js"
}
if come to any error replace this line "dev": "nodemon -r dotenv/config --experimental-json-modules src/index.js" with this "dev": "nodemon -r dotenv/config src/index.js"
My index.js file
import dotenv from "dotenv";
import express from "express";
const app = express();
dotenv.config({
path: "./.env",
});
in the script dev line src/index.js this is the path of your index.js file.
NOW YOU CAN ABLE USE YOU .env FILE GLOBAL. THANKS;