Cannot pass .env Challenge in Basic Node even though everything is set up correctly

Tell us what’s happening:
I cannot pass the challenge even though I have done everything right in repl.it
I have already installed dotenv package and also .env file but freecodecamp still marks it as fail. And I can access the endpoint successfully as well.

Below is my code in myApp.js

Your code so far

require('dotenv').config();
var express = require('express');
var app = express();

//middleware for all requests stuff (root level)
app.use(express.static(__dirname + "/public"));

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

app.get("/json", (req, res) => {
  let message = (process.env.MESSAGE_STYLE=="uppercase") ?
  "HELLO JSON":
  "Hello json"

  res.json({"message":message})
})

Your browser information:

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

Challenge: Use the .env File

Link to the challenge:

Link to the repl.it:
https://repl.it/join/tdhqcopg-moonlightkim

I think that the problem is that you are hard-coding the response object. You need to use JS to manipulate the response into uppercase when your dotenv variable is set. Don’t hardcode “HELLO JSON”.

From instructions (important in bold):
Store the variable MESSAGE_STYLE=uppercase in the .env file. Then tell the GET /json route handler that you created in the last challenge to transform the response object’s message to uppercase if process.env.MESSAGE_STYLE equals uppercase . The response object should become {"message": "HELLO JSON"} .

I hope that helps :slight_smile:

I’ve already used toUpperCase() function but it still didn’t work. When I submit the answer, it shows this syntax error: “SyntaxError: Unexpected token < in JSON at position 0”

I don’t know where that syntax error is coming from. I’m going to work through that portion of the curriculum real quick again since when I did it it was on glitch and see if I can get things to pass… if so then I’ll diff all of the files and see what is different

1 Like

Hello there,

Your issue sounds similar to: Headers Error in .env Challenge - JavaScript - The freeCodeCamp Forum

We are still trying to find the source of the issue, because some Campers experience it, whilst others do not.

EDIT: As an update, code that passed 3 days ago is now failing, and this challenge has not been changed since, neither has the testing. So, I suspect it might be related to Repl.it

1 Like

Okay @Sky020 . I’m looking forward to any updates related to this issue.

I can confirm after going through the updated curriculum that I am encountering the same issue. Do we have data on who has passed it and when they forked the project? I’m suspecting there was a change in the dependency chain somewhere.

Currently, we have noticed a change in the way Repl.it handle certain request parameters, and redirects:
Use the .env File Challenge Issue · Issue #40915 · freeCodeCamp/freeCodeCamp (github.com)

I was pair-programming with someone who passed it. So, the code was identical.