You can specify the path where the .env file is to dotenv package.
Please modify the following line of code to
require("dotenv").config();
like this:
require("dotenv").config({path: "./back-end/.env"});
write PORT in .env file in backend folder
PORT = <PORT_NUMBER>
For Ex.
PORT = 3000
Install dotenv in backend folder
npm i dotenv
Import dotenv in server.js file
require('dotenv').config()
or set .env path in
require("dotenv").config({path: "./back-end/.env"});
Then Check by:
console.log(process.env);
Run in backend folder
node server.js
To use DotEnv, first install it using the command: npm i dotenv . Then in your app, require and configure the package like this: require('dotenv'). config()
require('dotenv').config();
console.log(process.env.MY_ENV_VAR);