Ali
Ali

Reputation: 23

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

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 ...

Upvotes: 2

Views: 1774

Answers (2)

Bruno Peres
Bruno Peres

Reputation: 3246

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

Upvotes: 3

Ali Azmoodeh
Ali Azmoodeh

Reputation: 191

One of the first things you should pay attention to is the file path. Check where your .env file is located in the project. If it is in the root of the project, there is no need to do anything. Otherwise, you must specify the path to the .env file.

Upvotes: 3

Related Questions