Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dotenv module in node js return undefined

Tags:

node.js

dotenv

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

like image 774
mitul013 Avatar asked May 19 '26 11:05

mitul013


1 Answers

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'})

like image 114
Schutt Avatar answered May 21 '26 00:05

Schutt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!