Socket.io: Server.prototype.serveClientはwebpackでは機能しません

作成日 2017年07月29日  ·  4コメント  ·  ソース: socketio/socket.io

  • [x]バグを報告する

現在の動作

この関数は相対パスを使用して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で問題なくコンパイルされ、スムーズに実行されるはずです。

設定

  • OS:Windows
  • ブラウザ:N / A
  • socket.ioバージョン:
    "webpack": "^ 3.3.0"
    "socket.io": "^ 2.0.3"、
    "socket.io-client": "^ 2.0.3"

    その他の情報(スタックトレース、関連する問題、修正方法の提案など)

最も参考になるコメント

socket.ioの_servingclient files_feat。を無効にしてみてください。

const io = require('socket.io')(httpServer, { serveClient: false })

理想的ではありませんが、少なくともバンドルは機能します。

全てのコメント4件

同じ問題

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');
     }

これが私のために働いてくれてありがとう。

このページは役に立ちましたか?
0 / 5 - 0 評価