Advanced Node and Express - Set up the Environment

Why would the comment /*global io*/ “suppress any errors”?

Since when do comments have any functionality besides providing information to the programmer reading the code?

This instruction is in https://learn.freecodecamp.org/information-security-and-quality-assurance/advanced-node-and-express/set-up-the-environment

When I follow these instructions I get “client.js:3 Uncaught ReferenceError: io is not defined” which I would expect, since io really isn’t defined in the client.js file. But the instructions are telling me not to define io in that file but just to write comment global io which I can’t imagine actually does anything.

The technique of setting flags or markups in comments in the middle of code is actually somewhat common. One example I can give is in Java where the Netbeans IDE e.g. looks for certain error or warning suppression flags in the comments of every Class Object. This is because making these compiler/IDE/Framework specific tricks or features into a standard part of the language is difficult as the language specification has to be expanded to accommodate these features that can vary wildly and be highly specific to the needs of just a few persons. As we know, since languages are so broad, it takes more people coming together to decide the fate of such things, and the more people involved the longer it takes to decide on something.

If instead the special needs are accommodated by placing them in comments it does not break the main code since all other systems reading that code will simply ignore these comments, makes it compliant to standard specifications of the language, speeds up implementation since no one has to sit in committees, and only requires the specific devs working with or needing that specific feature/hook/whatever to learn the syntax. It’s a very useful thing to have, especially since a language’s or framework’s developers cannot see in to the future and predict every single way that their product will be used.

Interesting. Good to know.

For this particular issue though, I’m still wondering how to deal with “Uncaught ReferenceError: io is not defined” … perhaps the javascript engine in chrome doesn’t know that this comment should suppress that error?

The sample code is https://gist.github.com/JosephLivengood/41ba76348df3013b7870dc64861de744

It’s not to do with the JS engine, it’s to do with the linting package ESLint, which is often added to JS projects. When you run ESLint on a JS file, it can use a set of configurable rules to check for errors. One of those rules is to disallow globals. Normally that would apply, so the io variable being global would be flagged as a possible programmer error. But you can override rules on a per-file basis by putting comments (written in a specific way) in the file. ESLint will see this and ignore the io variable. If ESLint isn’t running (ie anywhere except a project under development that has ESLint included), then nothing happens - it’s just a comment, and comments mean nothing when JS is ran - io is a global variable, and JS is not going to stop you using global variable.

4 Likes

Thank you! Does that mean ESLint should be included in the Glitch starter package for this assignment, but isn’t?

So the error isn’t because of this, it’s because io literally isn’t defined: when you do var socket = io();, io should be the function thay was imported (const io = require('socket.io')(http);). Something is missing here because your JS has no idea what the the function io that you’re trying to execute is

Thanks for your help. I’m still not getting this though. I think I’ll need to take some courses on Udemy to understand all this more in-depth. I’ve done all the FCC certificates except for this last one. I’ll wait to do that once there have been some major updates to this cert.