该函数使用相对路径来检查 socket.io-client。 但是,如果我使用 webpack 捆绑使用 webpack 的 nodejs 应用程序。 这将成为 node_modules 中 socket.io-client 的相对路径。
然后运行节点会返回一个错误:
错误:找不到模块“。”
注意:获得快速答案的最佳方法是提供一个失败的测试用例,例如通过分叉以下小提琴。
包中的编译代码变为:
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;
};
该包应该可以与 webpack 一起编译并顺利运行。
同样的问题
尝试禁用 socket.io 的 _serving client files_ 壮举。
const io = require('socket.io')(httpServer, { serveClient: false })
不理想,但至少捆绑有效。
我认为这是同样的错误,我通过使用 webpack 进入运行时
Error: Cannot find module 'socket.io-client/dist/socket.io.js'
我不使用socket.io
self,但它是androidjs
的依赖
如果我在后续补丁中使用raw-loader
,它会起作用
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');
}
谢谢这对我有用。
最有用的评论
尝试禁用 socket.io 的 _serving client files_ 壮举。
const io = require('socket.io')(httpServer, { serveClient: false })
不理想,但至少捆绑有效。