I can't access my .env variables!! nodeJS

Issue

Server is running on PORT: undefined in undefined mode.
MongoDB database connected with HOST: localhost


used to get Server is running on PORT: 4000 in development mode. and suddenly it’s undefined for both and every variable in my .env, like cloudinary .. stripe …

Solution

You can add this snippet before starting mongoDB to see what’s getting to your node process env variables:

console.log(process.env)

Also note that you can pass inline environment variables to node process like this:

PORT=4000 node ./my-script.js

To automatically load .env files, you might be using this package: https://github.com/motdotla/dotenv so be sure that you have all packages installed (npm install or yarn install, depending on what you are using).

Answered By – Bruno Peres

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published