question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use the .env File Challenge Issue

See original GitHub issue

For more coverage, I am opening this here:

Challenge: https://www.freecodecamp.org/learn/apis-and-microservices/basic-node-and-express/use-the--env-file

As of <04/02/2021, a change has caused the above mentioned lesson to be uncompletable.

Error: SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) at /home/runner/boilerplate-express-2/node_modules/fcc-express-bground/index.js:91:26

The error in full:

Error
SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at /home/runner/boilerplate-express-2/node_modules/fcc-express-bground/index.js:91:26
    at IncomingMessage.<anonymous> (/home/runner/boilerplate-express-2/node_modules/fcc-express-bground/index.js:33:7)
    at IncomingMessage.emit (events.js:315:20)
    at IncomingMessage.Readable.read (_stream_readable.js:505:10)
    at flow (_stream_readable.js:1005:34)
    at resume_ (_stream_readable.js:986:3)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)
SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at /home/runner/boilerplate-express-2/node_modules/fcc-express-bground/index.js:99:28
    at IncomingMessage.<anonymous> (/home/runner/boilerplate-express-2/node_modules/fcc-express-bground/index.js:33:7)
    at IncomingMessage.emit (events.js:315:20)
    at IncomingMessage.Readable.read (_stream_readable.js:505:10)
    at flow (_stream_readable.js:1005:34)
    at resume_ (_stream_readable.js:986:3)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)
_http_outgoing.js:518
    throw new ERR_HTTP_HEADERS_SENT('set');
    ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:518:11)
    at ServerResponse.header (/home/runner/boilerplate-express-2/node_modules/express/lib/response.js:771:10)
    at ServerResponse.send (/home/runner/boilerplate-express-2/node_modules/express/lib/response.js:170:12)
    at ServerResponse.json (/home/runner/boilerplate-express-2/node_modules/express/lib/response.js:267:15)
    at /home/runner/boilerplate-express-2/node_modules/fcc-express-bground/index.js:108:15
    at IncomingMessage.<anonymous> (/home/runner/boilerplate-express-2/node_modules/fcc-express-bground/index.js:33:7)
    at IncomingMessage.emit (events.js:315:20)
    at IncomingMessage.Readable.read (_stream_readable.js:505:10)
    at flow (_stream_readable.js:1005:34)
    at resume_ (_stream_readable.js:986:3) {
  code: 'ERR_HTTP_HEADERS_SENT'
}

If you encounter this, the best course of action is to skip the lesson, and continue with the curriculum.

As nothing obvious has changed on freeCodeCamp’s side, this is likely an issue on Repl.it’s side.

Current Debugged Steps
  1. Tested code which previously used to pass - no longer passes
  2. Downgraded express package - no effect
  3. One user was able to pass, and the noticeable difference was their origin was not freecodecamp.org, when they submitted.
  4. All manor of sending data via stringifying etc.

From forum post: https://forum.freecodecamp.org/t/headers-error-in-env-challenge/444357

_P.S. Even though I have said there has been no change to the challenge, in the last few seconds (19:00 UTC) there has been a change pushed to main. Otherwise, our side has not changed in a way I can see which would affect this

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:15 (15 by maintainers)

github_iconTop GitHub Comments

3reactions
gikfcommented, Feb 7, 2021

I believe I’ve found what’s happening, although not fully sure why.

Long story short - request send from ie. /_api/use-env-vars to the relevant /json route uses http, what results in (repl.it?) trying to redirect to https. I don’t know how up to date this information is, but I’ve found posts that http.request doesn’t follow redirects on it own.

https://github.com/freeCodeCamp/fcc-express-bground-pkg/blob/master/index.js#L16

var selfCaller = function (path, req, res, cb, url) {
  var url = req.get('host').split(':');
  var port = url[1];
  url = url[0];
  var prot = req.protocol === 'https' ? https : http;
  var opts = {
    hostname: url,
    method: 'GET',
    path: path,
    port: port || 80,
    headers: {
      'User-Agent':
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
    },
  };
  var rq = prot.request(opts, function (r) {
(...)

Taking repl.it as example: req.host doesn’t contain port, so port is undefined and in the request will be always used 80. Second thing is that protocol in passed req is http. Changing prot to https and port to 443 makes everything work.

FWIW this is affecting also Basic Node and Express - Implement a Root-Level Request Logger Middleware and Basic Node and Express - Chain Middleware to Create a Time Server.

1reaction
ShaunSHamiltoncommented, Feb 19, 2021

@JoshuaPelealu There is a temporary solution, just waiting to be QAed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use the .env File -- challenge is not accepting solution
My issue is that I've written this code several different (correct) ways in javascript and the challenge is not recognizing the correctness of ......
Read more >
Basic Node & Express - 06 Use the .env File - YouTube
FreeCodeCamp lesson walkthrough - explained.To assist you if you get stuck, and breakdown core concepts the material is trying to convey.
Read more >
Why syncing .env files doesn't scale for secrets management
Using .env files allowed us to move secrets out of source code. Unfortunately, they introduced a new set of challenges: Scaling issues ......
Read more >
A Gentle Introduction to .env Files | by Tony - Bits and Pieces
To summarize, .env files contain credentials in key-value format for services used by the program they're building. They're meant to be stored locally...
Read more >
Using Environment Variables | Cloud Functions Documentation
gcloud functions deploy. FUNCTION_NAME --set-env-vars FOO=bar,BAZ=boo. FLAGS ... If you want to store your configuration in a file (e.g. under source control), ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found