Basic Node and Express - Use the .env File

UPDATE: finally managed to solve, will update later. No need for anyone to waste their time trying to help, unless you feel like doing a little debugging practice!

Half the time when I post here, the solution simply comes to me about 30 seconds after posting, so here’s hoping. Anyway, I’ve been stuck with this one for a while and I just can’t figure out what is wrong (seeing as ‘{“message”: “Hello json”}’ does display, I’m guessing that process.env.MESSAGE_STYLE is not being accesed for some reason, but everything looks OK, the file is correctly named and in the root folder. Any help will be much appreciated.

Exercise summary:

Create a .env file in the root of your project directory, and store the variable MESSAGE_STYLE=uppercase in it.

Then, in the /json GET route handler you created in the last challenge access process.env.MESSAGE_STYLE and transform the response object’s message to uppercase if the variable equals uppercase. The response object should either be {"message": "Hello json"} or {"message": "HELLO JSON"}, depending on the MESSAGE_STYLE value. Note that you must read the value of process.env.MESSAGE_STYLE inside the route handler, not outside of it, due to the way our tests run.

Failed test message:
" The response of the endpoint /json should change according to the environment variable MESSAGE_STYLE"

Relevant snippet.

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

My screen:

Project link:
solution: https://3000-freecodecam-boilerplate-2bhim2a2ctj.ws-us108.gitpod.io

Your browser information:

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

Challenge Information:

Basic Node and Express - Use the .env File

Hi! Can you tell me how you got your problem fix? I’m ocurring the same problem right now and still can’t figure out.

We need to see your code, please open your own thread.

You should have a .env file with the variable/value and access it as explained in the challenge. If you are running on Replit they have a Secrets option you must use instead.

In my case, the problem was that I had added the variable to the process.env file. I didn’t know you can create a file with an extension and no filename, so since the lesson tells you to use process.env.VAR_NAME , I created a file called process.env

In reality, I just needed to create a fille named .env that has no name and add the variable name there.

Thanks for your reply. For some magical reason when I open the challenge again today it just work.
I did create a .env file and put the VARIABLE/value there, I even went as far as going to the user setting and add an environment variable but it didn’t work. Good thing it all settles now.

1 Like