Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find module 'socket.io-client/dist/socket.io.js' when starting express application

I'm having quite a frustrating issue with something that seems trivial.

I'm running an express application on a node.js server, written in ES6 and compiled using webpack. It compiles without any errors except from the following warning:

../node_modules/socket.io/lib/index.js 113:11-32
0:0  warning  Critical dependency: the request of a dependency is an expression

(although I'm not sure whether this is linked to my current problem)
However, when I start the server I get the following error:

Error: Cannot find module 'socket.io-client/dist/socket.io.js'
at Function.webpackEmptyContext [as resolve] (webpack:///../node_modules/socket.io/lib_sync?:2:10)
at resolvePath (webpack:///../node_modules/socket.io/lib/index.js?:113:100)
at Server.serveClient (webpack:///../node_modules/socket.io/lib/index.js?:116:25)
at new Server (webpack:///../node_modules/socket.io/lib/index.js?:53:8)
at Function.Server [as listen] (webpack:///../node_modules/socket.io/lib/index.js?:44:41)
at new Socket (webpack:///./server/socket.js?:10:98)

Socket class:

import io from 'socket.io';

export default class Socket {
    constructor(server) {
        this.io = io.listen(server);

        this.io.on('connection', (socket) => {
            // Handle connection
        });
    }
}

The server parameter is the object returned from the express.listen() function.

I installed socket.io using the command npm install socket.io --save, subsequently adding the dependency "socket.io": "^2.1.1" in my package.json, and ran the command npm install to update my dependencies but still encounter the error.

As you can tell, I'm rather puzzled by this... especially after following the docs almost exactly here so any help is thoroughly appreciated!

like image 341
dodgewhale Avatar asked Aug 19 '18 03:08

dodgewhale


1 Answers

This did the trick on my side:

var io = require('socket.io')(server, { serveClient: false })
like image 187
IgnacioHR Avatar answered Nov 19 '22 13:11

IgnacioHR