"Cannot find module 'http'"?

I need to load in the npm module socket.io. I have npm install'ed it. My app uses React, and if I follow correctly, I need to load socket.io from inside a particular component. Following these docs:

… I’ve tried the following at the top of the component:

var io = require('socket.io')(http, { serveClient: false });

I’ve also tried:

var io = require('socket.io')();
io.serveClient(false);
io.attach(http);

I’m getting this console.log error:

Uncaught Error: Cannot find module 'http'

I’ve run npm install http, but I’m still getting this console error.

How can I correct this?

Thanks very much in advance to all for any info.

It looks like you are trying to use an undefined global?

Take a look at this example: https://github.com/Akryum/meteor-socket-io

Thanks very much! That worked. For reference, here’s the code I used.

npm install:
$ meteor npm install --save socket.io-client
$ meteor npm install --save http
$ meteor npm install --save http-browserify
$ meteor npm install --save meteor-node-stubs index.js bufferutil utf-8-validate

In client:

import Response from 'meteor-node-stubs/node_modules/http-browserify/lib/response';
if (!Response.prototype.setEncoding) {
    Response.prototype.setEncoding = function(encoding) {
        // do nothing
    }
}

// Socket io client
const PORT = 8080;
let socket = require('socket.io-client')(`http://localhost:${PORT}`);

socket.on('connect', function() {
    console.log('Client connected');
});
socket.on('disconnect', function() {
    console.log('Client disconnected');
});
1 Like