Search code examples
node.jsexpressserverenvironment-variablesdotenv

environment variables not working in node js server


When i set my username and password directly in a nodemailer server, it works as expected

auth: {
    user: 'myusername',
    pass: 'mypassword'
},

But on using dotenv, nothing happens

require ('dotenv').config();

auth: {
  user: process.env.USERNAME,
  pass: process.env.PASSWORD
},

I ran npm install dotenv on server side and set up a .env file with the variables below:

USERNAME:myusername

PASSWORD:mypassword


Solution

  • The syntax in your .env file is incorrect. Use equals = signs rather than colon :.

    USERNAME=myusername
    PASSWORD=mypassword
    

    See also: https://www.npmjs.com/package/dotenv