Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Express Basics Serving Static Files in Express Merging the Design Files

Jack Roboski
PLUS
Jack Roboski
Courses Plus Student 4,999 Points

RangeError: Invalid status cod: undefined

Hello everyone.

I was following along and everything was going great until the final video in this course. I decided to undercut my problem solving for the sake of time (copy pasting from html2jade to Visual Code and changing the placeholders was taking a while) and copy exactly what Mr. Chalkley was showing on this video to see the final results. The first snag was that the copy-pasting was causing format issues with my tabs suddenly generating 4 spaces in Visual Code (but only sometimes?). After fixing that, I had an issue with a RangeError as my title suggests.

I have no idea how to fix it, the Docs are brief on what it is and I cannot find any values that are outside of a valid "range". Like I said, all I changed from previous videos to this one was exactly what was shown in the video, word for word, but now my project cannot do anything at all. Was there something not mentioned in the video that had to be changed in order for the syntax in these videos to function?

Jack Roboski
Jack Roboski
Courses Plus Student 4,999 Points

I downloaded the finished product, and, after updating the package on the provided completed challenge, it runs perfectly fine. So I am trying to comb through for differences. I am still at a loss for what possibly could be causing this RangeError. I added in the "/static/js/app.js" file and this removed the RangeError from the home URL (apparently RangeError also means you're calling a non-existent script?), but if you click any buttons, the server runs into the RangeError again. I have looked at my routes, my pug files, both my app.js files, and the index.js file while comparing them side by side to the completed challenge files. I cannot find anything invalid between the two. I hope I'm the only person who managed to do this because, until the very end, this was a great course. In hindsight, I should have backed up my original project before blindly following word for word just to save time. I assumed I could not hit an unintelligible error going word for word haha

My only guess so far is that there is something that seems correct, but is not, and is causing a seemingly irrelevant error (RangeError). Considering the first error being that I did not add the app.js file to the correct directory, this could mean that there is something else not quite named correctly that I'm referencing. I really wish the error made sense so I could find it.

2 Answers

Jessica Beech
Jessica Beech
11,405 Points

Hey! I had this problem and it drove me insane! I posted a similar response in another thread here.

Basically the reason you're probably getting a RangeError is because pug rendering errors don't seem to create sensible status codes and the Error handling function in app.js assumes you have a normal status code when producing the error page!

So when the error occurs, you don't get the nicely designed error page, nothing renders, and you have no idea what you did wrong – so frustrating!

Change the error handling function in app.js to the following:

app.use(( err, req, res, next ) => {
  res.locals.error = err;
  if (err.status >= 100 && err.status < 600)
    res.status(err.status);
  else
    res.status(500);
  res.render('error');
});

This should mean you'll get an error page load, with the stack trace that actually tells you where the problem is! Mine said it was an indentation thing in my card.pug file - similar to you and not unsuprising given the hazards of html2jade.

Hope this helps you and anyone else who gets to the end of this series and has a similar issue!

Nothing more annoying than getting stumped at the final hurdle.

Gabbie Metheny
Gabbie Metheny
33,778 Points

Both this solution and Abraham's got rid of the range error in my console, but adding this conditional to the error handler gave me sensible error messages again! Thanks, Jessica!

Abraham Juliot
Abraham Juliot
47,353 Points

I was seeing a similar error. This may help:

change app.use('/static', express.static('public')) to app.use('/static', express.static(__dirname+'/public'))