Unable to pass the test in use the .env file

Tell us what’s happening:
unable to run the test after install the npm install dotenv ,

Your code so far

Your browser information:

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

Challenge: Use the .env File

Link to the challenge:

Have you created a .env file?

Yes, done already but still not passing

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

});
required('dotenv').config();

above code I did for the app,js file and for the .env file, I did : MESSAGE_STYLE === “uppercase”;

I think you need to read again the description on how the .env file needs to be written

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).


Also, this is not the correct way to require and use dotenv:

  • It should not be at the bottom of the file. It should be configured above any use of process.env
  • The syntax is slightly off.

Hope this helps

Thanks for all the suggestions . I digged further into the topic and this is what I came up with in my code files, yet, I am getting a bug, please help.Thanks
in my App.js file

let express = require('express');
const(request, response) = require('express');
require('dotenv').config();
let app = express();
console.log(process.env.MESSAGE_STYLE);

//let app = express();
//console.log(process.env//.VARIABLE_ONE)


//app.use('/pics', express.static(__dirname + '/images'));
//console.log("Hello World");
/** app.get("/", (req,res)=>{
  res.send("Hello Express");

});*/

// app.get("/",function(req,res){
  //res.sendFile(__dirname + "/views/index.html");
//});

app.use(express.static(__dirname + "/public"));

//serve json on a specific route
let message = {"message": "Hello json"};
//app.get('/json',(request,response)=> {
  //response.json(message);
//});

/**6)use the .env File to configure app*/

//app.get("/", function(req, res){
  //res.sendFile(__dirname+ '/views/index.html');
// app.use(express.static(__dirname + "/public"));
//})
//7.use the .env file
app.get("/json", (request, response)=>{
  if(process.env.MESSAGE_STYLE === 'uppercase'){
     response.json({"message" : "HELLO JSON"})
  }else{
    response.json(message)
  }
});

and in my .env file

MESSAGE_STYLE = 'uppercase'

There should not be any spacing between the = (equals sign)

Hope this helps

also in your file you don’t need to add any quotes to denote it as string as it is read as string by default and maybe this is the last bug you would get in the if condition
ex:-

MESSAGE_STYLE=uppercase

Did this get resolved??

When i use dotenv i require it first… in express the order of ‘things’ is really important, I’ve had apps fail because i didn’t require dotenv first!

Finally I passed the test…I had commented out by mistake the code block for configuring the app hence I was not passing but now, all is well, I passed. Thanks to you and everyone for the suggestions.

Thanks, I passed now

Thanks, I passed now so, the problem is solved

I passed the test, thanks