Patricio Vargas
Patricio Vargas

Reputation: 5522

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 

Upvotes: 18

Views: 49489

Answers (8)

David Aniebo
David Aniebo

Reputation: 283

Create or ensure you have a nodemon.json file in the root directory of your project (where your package.json is located). Inside nodemon.json, define your environment variables like this:

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

Restart your server using the nodemon command (or the script you’ve defined in package.json). With this setup, nodemon will automatically load your environment variables from the nodemon.json file.

Upvotes: 0

Rohit
Rohit

Reputation: 373

In node js version 21 or later, you have to use like :

import { loadEnvFile } from 'process';

loadEnvFile('.env');

Suppose if you have .env file that contains a variable such as: SecretKey=taW4iLCJJc3N1ZXIiOiJJc3N1ZXIiLCJVc2V then you have to read this variable like this :

console.log(process.env.SecretKey)

Upvotes: 1

internetdrew
internetdrew

Reputation: 79

Sure someone at some point will run into this headache somehow. I had the issue of not being able to read these variables even though I absolutely had everything exactly as it needed to be (and I didn't even need the dotenv package) in my .env file. I uninstalled dotenv and any other packages to help and values remained undefined until...

I deleted node_modules and did a fresh npm install.

Somehow, that fixed it based on a config issue I am sure I introduced at some point in the process. Hours of debugging came down to this solution.

Hope this saves someone else all of this time.

Upvotes: 0

Rajaruban Rajindram
Rajaruban Rajindram

Reputation: 1006

This file nodemon.json needs to be in the root directory of your project.

nodemon.json

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

Install dotenv version 10.0.0 in the file below; nodemon.json

"dependencies": {
    "dotenv": "10.0.0",
  },

index.js (assuming its your main server)

Finally import into your main server file (index.js) as below;

require("dotenv").config();

Upvotes: 1

gopinath krm
gopinath krm

Reputation: 121

The env variable do not contain the trailing white spaces and also remove the quotes

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

to

MONGO_ATLAS_PW=xxxx 
JWT_KEY=secret_this_should_be_longer

and restart the server

or you can also try using the nodemon.json - create a new file called nodemon.json in your root directory

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

and restart the server

for accessing the variable

process.env.MONGO_ATLAS_PW 
process.env.JWT_KEY

Upvotes: 2

Kris Burke
Kris Burke

Reputation: 451

I believe the nodemon.json file is only for setting nodemon specific configuration. If you look at the nodemon docs for a sample nodemon.json file, the only env variable they mention setting is NODE_ENV.

Have you considered putting these environment variables for your app in a .env file instead? There is a package called dotenv that is helpful for managing env variables in Node.

First, install dotenv using the command npm install dotenv

Then, create a file called .env in the root directory with the following:

MONGO_ATLAS_PW=xxxxx
JWT_KEY=secret_this_should_be_longer

Finally, inside your app.js file after your imports add the following line:

require('dotenv').config()

Upvotes: 19

Patricio Vargas
Patricio Vargas

Reputation: 5522

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"
  }
}

Upvotes: 5

Jack Bashford
Jack Bashford

Reputation: 44145

I believe you're referring to the dotenv package. To configure it, first create a file called .env with your keys and values stored like so:

MONGO_ATLAS_PW=xxxxx
JWT_KEY=secret_this_should_be_longer

Then, in your server.js, add this near the top:

require("dotenv").config();

Then the process.env variable will be an object containing the values in .env.

Upvotes: 12

Related Questions