تستخدم الوظيفة مسارًا نسبيًا للتحقق من وجود socket.io-client. ومع ذلك ، إذا كنت أستخدم حزمة الويب لتجميع تطبيق nodejs يستخدم حزمة الويب. سيصبح هذا هو المسار النسبي ل socket.io-client في node_modules.
ستظهر العقدة قيد التشغيل بعد ذلك خطأ:
خطأ: لا يمكن العثور على الوحدة النمطية "."
ملحوظة : أفضل طريقة للحصول على إجابة سريعة هي تقديم حالة اختبار فاشلة ، عن طريق تزييف الكمان التالي على سبيل المثال.
يصبح الرمز المترجم في الحزمة:
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's _serving client files_.
const io = require('socket.io')(httpServer, { serveClient: false })
ليست مثالية ، ولكن على الأقل الحزمة تعمل.
أعتقد أن هذا هو الخطأ نفسه ، فأنا أحصل على وقت التشغيل باستخدام حزمة الويب
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's _serving client files_.
const io = require('socket.io')(httpServer, { serveClient: false })
ليست مثالية ، ولكن على الأقل الحزمة تعمل.