Home > OS >  Node Server .env file undifined in server.js
Node Server .env file undifined in server.js

Time:07-25

The below screenshot shows my file structure in VS code. I have used npm concurrent to run both backend and frontend simultaneously. My problem is env file is not recognized in the server file. Please tell me what is wrong here.

enter image description here

Second picture

enter image description here

CodePudding user response:

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"});

CodePudding user response:

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);

CodePudding user response:

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
  • Related