Posting my solution for problem encountered with .env

I faced issues using .env file for the task " Use the .env File" and after many hours of debugging I finally able to resolve. Here are the solution that might help others:

  1. Make sure you have installed dotenv module.
npm i dotenv
  1. Add the dotenv module in myApp.js
const path = require('path')
require('dotenv').config({ path: path.resolve(__dirname, './.env') });

Your code so far

app.get("/json", function (req, res) {
  if (process.env.MESSAGE_STYLE === 'uppercase') {
    res.json({
      "message": "HELLO JSON"
    });
  }
    res.json({
      "message": "Hello json"
    });  
});

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36

Challenge: Use the .env File

Link to the challenge:

Hi @James7000 !

Congrats on completing the challenge but we don’t allow forum users to post full working solutions.

I have added [spoiler] tags around your code.

Thanks!

1 Like