question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Server.prototype.serveClient does not work with webpack

See original GitHub issue
  • 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)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
bigchirvcommented, 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.

1reaction
ghostcommented, 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');
     }
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use the webpack-dev-server/client/web_modules ...
Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues...
Read more >
node.js - WebPack and Express server not working together
I'm wanting to use webpack for babel to use es6 when writing react and use its hot-module and cheap-module-source-map. But, webpack runs it's ......
Read more >
Zero Config JavaScript App Prototyping with Webpack - Auth0
Then, create a folder named webpack-prototype and make it your current working directory. You can do this easily with the following command:
Read more >
Development - webpack
webpack is a module bundler. Its main purpose is to bundle JavaScript files for usage in a browser, yet it is also capable...
Read more >
HMR and Hot Reloading with the webpack-dev-server
"ReferenceError: window is not defined" (if hmr is true); "TypeError: Cannot read property 'prototype' of undefined" (if inline is true). Client Side rendering ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found