Search code examples
javascriptnode.jsjsonnodejs-server

NodeJS environment variables undefined


I'm trying to create some envrioment variables but when I create the file and run the server the seem to be undefined. I'm using nodemon. I have restarted my server and no luck.

UPDATED

.env

MONGO_ATLAS_PW = "xxxx";
JWT_KEY = "secret_this_should_be_longer";

package.json

...
  "scripts": {
    ...
    "start:server": "nodemon ./server/server.js"
  }

app.js

 require('dotenv').config();
 ...
 console.log(process.env.JWT_KEY); //undefined 

Solution

  • This needed to be in the root directory of my project.

    nodemon.json

    {
      "env": {
        "MONGO_ATLAS_PW": "xxxx",
        "JWT_KEY": "secret_this_should_be_longer"
      }
    }