Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server.prototype.serveClient does not work with webpack #3017

Closed
1 task done
sdachen opened this issue Jul 29, 2017 · 5 comments
Closed
1 task done

Server.prototype.serveClient does not work with webpack #3017

sdachen opened this issue Jul 29, 2017 · 5 comments
Labels
documentation Improvements or additions to documentation

Comments

@sdachen
Copy link

sdachen commented Jul 29, 2017

  • 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)

@mrdulin
Copy link

mrdulin commented Sep 12, 2017

same issue

@chirvo
Copy link

chirvo commented Jan 26, 2018

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.

@ghost
Copy link

ghost commented Aug 20, 2019

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');
     }

@jester7
Copy link

jester7 commented Apr 1, 2020

Thanks this worked for me.

@darrachequesne
Copy link
Member

For future readers: please check https://socket.io/docs/v4/server-with-bundlers/

@darrachequesne darrachequesne added the documentation Improvements or additions to documentation label Jun 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

5 participants