Basic Node and Express - Use the .env File

Tell us what’s happening:
I am not able to pass the .env lesson. It works when run on a browser. But When I paste the link for validating. It does not pass.

and this is what I get

// running tests The response of the endpoint json should change according to the environment variable MESSAGE_STYLE

// tests completed 
// console output 
[Error: Not Found]

**Your project link(s)**


solution: https://replit.com/@tanmay1978/nodejson

**Your browser information:**

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

**Challenge:** Basic Node and Express - Use the .env File

**Link to the challenge:**
https://www.freecodecamp.org/learn/back-end-development-and-apis/basic-node-and-express/use-the--env-file

Hi there and welcome to the forum!

This is not a valid repl link. It gives me a 404 error.

The instructions:

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

Why do you have this in your code?

if (process.env.MESSAGE_STYLE === "allCaps")

Also:

The response object should either be {"message": "Hello json"}

You are returning this (which is not quite the same):

"message": "Hello Json"

Finally, ensure that you are submitting the repl live link to FCC (rather than the link to your code). This is the link which is directly above the webview window.

.env already exists, corrected my if condition to check for “uppercase”.
The if condition is to check if the .env variable has the word “uppercase” then change the message value to all capitals else should be default sentence case.

So you have passed now? BTW, I should have pointed out the bit in the instructions where it says that you can’t use a .env with Replit. You put the variable in your Secrets tab instead.

I have added the variable in the Secrets tab as well. But still it won’t pass

I forked your code and it passes for me when I add the variable to the Secrets tab.

You should be adding it as a key/value pair, MESSAGE_STYLE and uppercase. No quotation marks around either and remember to click Save.

yeah but when I past the solution link it does not pass. If opened in a seperate browser window and I try it my code works.

When I visit https://node.tanmay1978.repl.co/json, I get {"message":"Hello json"}. This means that your code is not responding to an environment variable.

If you have the environment variable saved correctly in your Secrets tab then visiting that url should get me {"message":"HELLO JSON"}.

That’s because I had changed the value in the secrets tab to check. But I have reverted now.

You can check now.


I get this error

As the error indicates, the link you should be submitting to FCC is the root for your live project: https://node.tanmay1978.repl.co/

However, I checked again and the json route is still giving me the same response (i.e. it’s not responding to an environment variable).

I know your code works because when I fork your repl and put the correct environment variable in the forked version, it passes immediately.

You have no .env file (or sample.env) anywhere in your repl, correct?

I have .env file but it is not visible. I have used secret tab to create a KEY. If I change the KEY then I need to restart in order to reflect the change.

The reason for not working when you checked is because I had miss-spelled “uppercase”.

But it should work now if you test it.

Just checked it passed

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.