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 "."
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;
};
The package should compile with webpack just fine and run smoothly.
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.
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.