La función usa una ruta relativa para verificar socket.io-client. Sin embargo, si uso webpack para empaquetar una aplicación nodejs que usa webpack. Esta se convertirá en la ruta relativa a socket.io-client en node_modules.
El nodo en ejecución devolverá un error:
Error: no se puede encontrar el módulo "."
Nota : la mejor manera de obtener una respuesta rápida es proporcionar un caso de prueba fallido, bifurcando el siguiente violín, por ejemplo.
El código compilado en el paquete se convierte en:
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;
};
El paquete debería compilarse bien con webpack y funcionar sin problemas.
mismo problema
Intente deshabilitar la característica _serving client files_ de socket.io.
const io = require('socket.io')(httpServer, { serveClient: false })
No es ideal, pero al menos el paquete funciona.
Creo que este es el mismo error, me pongo en tiempo de ejecución usando webpack
Error: Cannot find module 'socket.io-client/dist/socket.io.js'
No uso socket.io
self, pero es una dependencia de androidjs
si uso raw-loader
con el siguiente parche, funciona
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');
}
Gracias, esto funcionó para mí.
Comentario más útil
Intente deshabilitar la característica _serving client files_ de socket.io.
const io = require('socket.io')(httpServer, { serveClient: false })
No es ideal, pero al menos el paquete funciona.