Advanced Node and Express: ReferenceError: io is not defined

Tell us what’s happening:
Describe your issue in detail here.
Regarding the Set Up Environment challenge, I’m getting issues with the use of the socket.io library.

If I strictly follow the steps mentioned in the challenge, I get the following when running the app:

"Looks like this page is being rendered from Pug into HTML! ReferenceError: io is not defined

Unable to login"

I don’t know where the problem exactly comes from.
If I hover on “scoket.io” in this line
const io = require("socket.io")(http);
I can see the following message.

Could not find a declaration file for module 'socket.io'. '/home/runner/boilerplate-advancednode/node_modules/socket.io/lib/index.js' implicitly has an 'any' type.
  Try `npm install @types/socket.io` if it exists or add a new declaration (.d.ts) file containing `declare module 'socket.io';`

After looking at this SO discussion, I replaced "socket.io": "~2.3.0" in the package.json file with "@types/socket.io": "~2.3.0".
However even after this change I got the same error ReferenceError: io is not defined.

In public/client.js, as requested in the challenge description I put

  /* global io */
  let socket = io();

My code is here: boilerplate-advancednode - Replit

Any help would be appreciated. I can’t complete the next challenges before fixing this issue.

Your browser information:

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

Challenge: Set up the Environment

Link to the challenge:

Hello there,

The issue is from when you did this step:

…in your routes.js file, add a GET route pointing to /chat which makes use of ensureAuthenticated , and renders chat.pug , with { user: req.user } passed as an argument to the response.

Click for hint:

There is something incorrect in this code.

app.get('chat', ensureAuthenticated, (req,res) => {
    res.render('pug/chat', {user: req.user});
  })

The type definition is not needed for the curriculum. It is just a suggestion given by the Replit Linter to allow you to have intellisense.

Hope this helps

Thanks for spotting one mistake.

Effectively I missed the forward slash in the endpoint, so it should be:

app.get('/chat', ensureAuthenticated, (req,res) => {
    res.render('pug/chat', {user: req.user});
  })

But unfortunately it does not solve the problem!

Ultimately I copied all the code provided in the solution here but I still get the ReferenceError.

You are calling io inside connection.js it should only be inside the DB connection in server.js (also you didn’t import it so it wouldn’t work anyway).

That is just type definitions you can’t replace the library with that (I know you didn’t in your current code I’m just letting you know).

Oh, I completely forgot to delete these lines of code after it became clear to me they should be placed inside the DB connection.

Well spotted!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.