SyntaxError: Unexpected token < in JSON at position 0

Tell us what’s happening:
When I run the tests I get “SyntaxError: Unexpected token < in JSON at position 0”

This is before I edit any code or if I do change it so the test should pass the same error occurs and the test never pass. If I manually hit my app /json URL there is no error and the output is as expected. It seems the test in this challenge is bugged and fails always. If I run the test in the previous or the next challenge the error does not occur and tests either fail or pass correctly depending on the state of the code.

Your code so far

app.get('/json', (req, res) => {
  let message = 'Hello json'
  if('uppercase' === process.env.MESSAGE_STYLE) {
    message = message.toUpperCase()
  }
  res.json({ message })
})

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 13020.55.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.77 Safari/537.36.

Challenge: Use the .env File

Link to the challenge:

Welcome, tluolamo.

You are not quite using res.json correctly. Have a read of the docs: https://expressjs.com/en/api.html#res.json

Hope this helps

I don’t see what you mean by not using the res correctly? I can literally copy the examples from the link to my code and the validation code still throws an error where expectation would be for it to return {"passed":false} if validations were to fail.

So again the /json end point works fine and returns the code as expected ie {"message":"HELLO JSON"}, but the validation api /_api/use-env-vars FCC uses to check the results is throwing an error. The error literally kills the process, whether I had anything changed from previous exercise or not, so no matter what I do it will always fail to validate.

Looking at this package which is used for the validation it seems it has not been updated in a while. So maybe it worked at some point but something has caused it to break on glitch side.

Also running the project locally (with appropriate adjustments like installing dotenv) seems to make the validation api work correctly, so it seems there is something specific to glitch that is causing the validation api to break.

I think there is an issue with Glitch (surprise, surprise…it might just be a temporary issue though). I get a 500 back and the json error as well on a project I know should pass.

It passes when using codesandbox
Edit: I broke it, let me see if I can get a new link
New link: https://g8b7s.sse.codesandbox.io


If you want you can try importing the gomix branch on codesandbox.

  1. You have to remove the engines from package.json

  2. Add the dotenv package, require it in and use it require('dotenv').config()

  3. Add the .env file with MESSAGE_STYLE=uppercase.

  4. Add your code back to myApp.js

You will likely have to restart the server, click the Server Control Panel icon on the left (it is the one that looks like a server rack), and then click the Restart Server button.

I can’t guarantee everything will work because I didn’t test it yet, I just did the .env challenge.

Edit: all the challenges pass for me using codesandbox. If you do want to use it for all the challenges I’d suggest installing nodemon, change the start script to "start": "nodemon server.js" and restart the server one last time manually. Now every time you make a change and save it should auto restart.

1 Like

Could you please post a link to your project?

Sure, here you go https://heavenly-subdued-cathedral.glitch.me/

Yeah codesandbox seems to work much better, now my validations are actually working and returning appropriately. Thanks!

1 Like