javascriptnode.jsenvironment-variables

Node Server .env file undefined in server.js


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 screenshot for illustration:

enter image description here


Solution

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