この関数は相対パスを使用して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の_servingclient files_feat。を無効にしてみてください。
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の_servingclient files_feat。を無効にしてみてください。
const io = require('socket.io')(httpServer, { serveClient: false })
理想的ではありませんが、少なくともバンドルは機能します。