Socket.io: Server.prototype.serveClient funktioniert nicht mit Webpack

Erstellt am 29. Juli 2017  ·  4Kommentare  ·  Quelle: socketio/socket.io

  • [x] einen Fehler melden

Aktuelles Verhalten

Die Funktion verwendet den relativen Pfad, um nach socket.io-client zu suchen. Wenn ich jedoch Webpack verwende, um eine nodejs-Anwendung zu bündeln, die Webpack verwendet. Dies wird der relative Pfad zu socket.io-client in node_modules.

Der laufende Knoten gibt dann einen Fehler zurück:

Fehler: Kann Modul "." nicht finden.

Schritte zum Reproduzieren (wenn das aktuelle Verhalten ein Fehler ist)

Hinweis : Der beste Weg, um eine schnelle Antwort zu erhalten, besteht darin, einen fehlgeschlagenen Testfall bereitzustellen, indem Sie beispielsweise die folgende Geige forken.

Der kompilierte Code im Bundle wird zu:

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

Erwartetes Verhalten

Das Paket sollte mit Webpack problemlos kompiliert werden und reibungslos laufen.

Konfiguration

  • Betriebssystem: Windows
  • Browser: N/A
  • socket.io-Version:
    "webpack": "^3.3.0"
    "socket.io": "^2.0.3",
    "socket.io-client": "^2.0.3"

    Sonstige Informationen (z. B. Stacktraces, verwandte Probleme, Vorschläge zur Behebung)

Hilfreichster Kommentar

Versuchen Sie, die _serving client files_ feat.

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

Nicht ideal, aber immerhin funktioniert das Bundle.

Alle 4 Kommentare

gleicher Fehler

Versuchen Sie, die _serving client files_ feat.

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

Nicht ideal, aber immerhin funktioniert das Bundle.

Ich denke, dies ist der gleiche Fehler, ich komme zur Laufzeit, indem ich Webpack verwende

Error: Cannot find module 'socket.io-client/dist/socket.io.js'

Ich verwende nicht socket.io self, aber es ist eine Abhängigkeit von androidjs

wenn ich raw-loader mit dem folgenden Patch verwende, funktioniert es

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

Danke das hat bei mir funktioniert.

War diese Seite hilfreich?
0 / 5 - 0 Bewertungen

Verwandte Themen

varHarrie picture varHarrie  ·  3Kommentare

doughsay picture doughsay  ·  4Kommentare

adammw picture adammw  ·  4Kommentare

kootoopas picture kootoopas  ·  4Kommentare

renkei picture renkei  ·  4Kommentare