mitul013
mitul013

Reputation: 78

dotenv module in node js return undefined

When i have written code for whatsapp boat using twilio module i get error in .env file

I have file structure like below:

/whatsappboatdemo
  | app.js
  | sendmsg.js
  | .env
  | package.json

And i have require dotenv like this in app.js file and sendmsg.js:

require('dotenv').config();

.env file content:

PORT=5000
TWILIO_ACCOUNT_SID=******************************

So when i run node app.js it gives undefined for process.env.PORT. I have tried with different file structure but i don't get idea so please help me in this , thanks in advance :)

Upvotes: 3

Views: 4893

Answers (2)

Schutt
Schutt

Reputation: 1134

It is really hard to debug your issue with what you provided. I would advise to check 3 things as these are common errors:

  • Check if the format of the .env file is correct: i.e.: PORT=9000 without any quotes etc.

  • Log the require call: console.log(require('dotenv').config())

  • Try to set the path manually and check if it works: require('dotenv').config({path: __dirname + '/.env'})

Upvotes: 10

brbn
brbn

Reputation: 71

It might be nice to see how and when you require dotenv module. Without further information I guess your error arise because when you are using your PORT variable, dotenv has not load it yet.

It is recommended to load dotenv module as soon as possible when loading your server.

Upvotes: 0

Related Questions