How to use Socket.io on glitch?

I created a chat app in my laptop and I’m trying to replicate it here on glitch but I’m getting errors.

Here’s how I referenced to socket. io in the index.html file

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.dev.js"></script>
<script src="/client.js"></script>

And here’s how I try to require socket.io in the server.js file. I get errors whenever I add the require(‘socket.io’) code.

const express = require('express');
const app = express();
const http = require("http").createServer(app);
const io = require('socket.io')(http);

Here’s the error I get and when I open the app I get a 504 error. Please help. Thanks!

image

Hello @evil-smiley,

Can you show us the code at server.js line 8 and is there anymore of the error that is not visible?

Hello @Callum-OKane

This is the code at line 8’
const io = require(‘socket.io’)(http);

And here’s the complete error message:
module.js:550

throw err;

^

Error: Cannot find module ‘socket.io

at Function.Module._resolveFilename (module.js:548:15)

at Function.Module._load (module.js:475:25)

at Module.require (module.js:597:17)

at require (internal/module.js:11:18)

at Object. (/app/server.js:8:12)

at Module._compile (module.js:653:30)

at Object.Module._extensions..js (module.js:664:10)

at Module.load (module.js:566:32)

at tryModuleLoad (module.js:506:12)

at Function.Module._load (module.js:498:3)

module.js:550

throw err;

^

Error: Cannot find module ‘socket. io’

at Function.Module._resolveFilename (module.js:548:15)

at Function.Module._load (module.js:475:25)

at Module.require (module.js:597:17)

at require (internal/module.js:11:18)

at Object. (/app/server.js:8:12)

at Module._compile (module.js:653:30)

at Object.Module._extensions..js (module.js:664:10)

at Module.load (module.js:566:32)

at tryModuleLoad (module.js:506:12)

at Function.Module._load (module.js:498:3)

Thanks @evil-smiley for that,

from what I can see here, you do not have the socketio module installed.
Complete these steps to install the socket.io module:

1. Go to your package.json file.
2. At the top left - There should be a button that says "Add Package" - Click it.
3. Into the search box -Type socket.io
4. Click socket.io when it shows up.
5. Your done! :) 

Can you come back to me and tell me if this resolves your issue!
These steps can also be used for installing other missing modules on your project!

1 Like

Thank you so much @Callum-OKane! It solved my issue :slight_smile:

1 Like

Glad I could be of assistance! :slight_smile:

Happy Glitching!