Socket.io: Server.prototype.serveClient does not work with webpack

Created on 29 Jul 2017  路  4Comments  路  Source: socketio/socket.io

  • [x] report a bug

Current behaviour

The function uses relative path to check for socket.io-client. However, if I use webpack to bundle a nodejs application that uses webpack. This will become the relative path to socket.io-client in node_modules.

Running node will then return an error:

Error: Cannot find module "."

Steps to reproduce (if the current behaviour is a bug)

Note: the best way to get a quick answer is to provide a failing test case, by forking the following fiddle for example.

The compiled code in the bundle becomes:

Server.prototype.serveClient = function(v){
  if (!arguments.length) return this._serveClient;
  this._serveClient = v;
  var resolvePath = function(file){
    var filepath = path.resolve(__dirname, './../../', file);
    if (exists(filepath)) {
      return filepath;
    }
    return /*require.resolve*/(!(function webpackMissingModule() { var e = new Error("Cannot find module \".\""); e.code = 'MODULE_NOT_FOUND'; throw e; }()));
  };
  if (v && !clientSource) {
    clientSource = read(resolvePath( 'socket.io-client/dist/socket.io.js'), 'utf-8');
    try {
      clientSourceMap = read(resolvePath( 'socket.io-client/dist/socket.io.js.map'), 'utf-8');
    } catch(err) {
      debug('could not load sourcemap file');
    }
  }
  return this;
};

Expected behaviour

The package should compile with webpack just fine and run smoothly.

Setup

  • OS: Windows
  • browser: N/A
  • socket.io version:
    "webpack": "^3.3.0"
    "socket.io": "^2.0.3",
    "socket.io-client": "^2.0.3"

    Other information (e.g. stacktraces, related issues, suggestions how to fix)

Most helpful comment

Try disabling socket.io's _serving client files_ feat.

const io = require('socket.io')(httpServer, { serveClient: false })

Not ideal, but at least the bundle works.

All 4 comments

same issue

Try disabling socket.io's _serving client files_ feat.

const io = require('socket.io')(httpServer, { serveClient: false })

Not ideal, but at least the bundle works.

I think this is the same error, I get on runtime by using webpack

Error: Cannot find module 'socket.io-client/dist/socket.io.js'

I don't use socket.io self, but it's a dependency of androidjs

if I use raw-loader by the follow patch, it works

diff --git a/node_modules/socket.io/lib/index.js b/node_modules/socket.io/lib/index.js
index 5287e4e..9ee4577
--- a/node_modules/socket.io/lib/index.js
+++ b/node_modules/socket.io/lib/index.js
@@ -113,9 +113,9 @@ Server.prototype.serveClient = function(v){
     return require.resolve(file);
   };
   if (v && !clientSource) {
-    clientSource = read(resolvePath( 'socket.io-client/dist/socket.io.js'), 'utf-8');
+    clientSource = require( 'raw-loader!socket.io-client/dist/socket.io.js');
     try {
-      clientSourceMap = read(resolvePath( 'socket.io-client/dist/socket.io.js.map'), 'utf-8');
+      clientSourceMap = require( 'raw-loader!socket.io-client/dist/socket.io.js.map');
     } catch(err) {
       debug('could not load sourcemap file');
     }

Thanks this worked for me.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thebinarypenguin picture thebinarypenguin  路  4Comments

adammw picture adammw  路  4Comments

renkei picture renkei  路  4Comments

stnwk picture stnwk  路  4Comments

varHarrie picture varHarrie  路  3Comments