Search code examples
node.jsexpressdotenv

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


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