【问题标题】:Cannot find module 'socket.io-client/dist/socket.io.js' when starting express application启动快速应用程序时找不到模块'socket.io-client/dist/socket.io.js'
【发布时间】:2018-08-19 03:26:06
【问题描述】:

我对一些看似微不足道的事情感到非常沮丧。

我在 node.js 服务器上运行一个 express 应用程序,用 ES6 编写并使用 webpack 编译。除了以下警告之外,它编译时没有任何错误:

../node_modules/socket.io/lib/index.js 113:11-32
0:0  warning  Critical dependency: the request of a dependency is an expression

(虽然我不确定这是否与我当前的问题有关)
但是,当我启动服务器时,出现以下错误:

Error: Cannot find module 'socket.io-client/dist/socket.io.js'
at Function.webpackEmptyContext [as resolve] (webpack:///../node_modules/socket.io/lib_sync?:2:10)
at resolvePath (webpack:///../node_modules/socket.io/lib/index.js?:113:100)
at Server.serveClient (webpack:///../node_modules/socket.io/lib/index.js?:116:25)
at new Server (webpack:///../node_modules/socket.io/lib/index.js?:53:8)
at Function.Server [as listen] (webpack:///../node_modules/socket.io/lib/index.js?:44:41)
at new Socket (webpack:///./server/socket.js?:10:98)

套接字类:

import io from 'socket.io';

export default class Socket {
    constructor(server) {
        this.io = io.listen(server);

        this.io.on('connection', (socket) => {
            // Handle connection
        });
    }
}

服务器参数是express.listen()函数返回的对象。

我使用命令npm install socket.io --save 安装了socket.io,随后在我的package.json 中添加了依赖项"socket.io": "^2.1.1",并运行了命令npm install 来更新我的依赖项,但仍然遇到错误。

如您所知,我对此感到相当困惑...尤其是在几乎完全按照 here 遵循文档之后,因此非常感谢任何帮助!

【问题讨论】:

  • 问题解决了吗?
  • 你有没有找到解决这个问题的方法?谢谢
  • @BillDagg 不,我最终决定改用这个 npmjs.com/package/ws 并且从那以后就没有看过 socket.io

标签: node.js ecmascript-6 socket.io


【解决方案1】:

这对我有用:

var io = require('socket.io')(server, { serveClient: false })

【讨论】:

  • 仍然收到同样的错误:(您还有其他想法吗?提前致谢
【解决方案2】:

socket.io 依赖于socket.io-client(因此它应该可用)。而documentation 可能已经解释了问题(很难说,因为没有相关代码)。

从 3.0 开始,快速应用程序已成为您传递给 httphttp Server 实例的请求处理函数。您需要将Server 传递给socket.io,而不是快速应用程序功能。还要确保在server 上调用.listen,而不是app

var app = require('express')();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
io.on('connection', function(){ /* … */ });
server.listen(3000);

【讨论】:

  • 感谢您的帮助,我已经修改了我的代码以创建一个 http 服务器并将该对象传递给套接字类,但我仍然遇到完全相同的错误。有任何想法吗? :(
【解决方案3】:

我终于解决了这个问题。我正在使用 nodejs 和 webpack 构建在 linux 中运行的服务器。使用 socket.io 作为 websocket 组件。我得到这个错误并去查看源代码:node_modules/socket.io/lib/index.js

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(file);
  };
  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;
};

所以,我更新了我的代码以禁用客户端: var io = require('socket.io')(http, {serveClient:false});

如果您想使用serveClient功能,您可以更新代码:

Server.prototype.serveClient = function(v){
    if (!arguments.length) return this._serveClient;
    this._serveClient = v;
   var resolvePath = function(file){
    var filepath = path.resolve(process.cwd(), './', file);
    if (exists(filepath)) {
      return filepath;
    }
    return require.resolve(file);
  };
  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;
};

然后将 socket.io-client 文件夹从 node_modules 复制到你的 webpack 构建路径。 请注意这是 webpack 的常见问题,当在第 3 部分库中使用 path.resolve 而不是 require() 加载资源时。总是,类似的问题应该逐案处理。

【讨论】:

    【解决方案4】:

    Webpack's official answer 是添加{ serveClient: false } 喜欢这里的人回答:

    var io = require('socket.io')(server, { serveClient: false })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-26
      • 2020-01-06
      • 1970-01-01
      • 2017-01-06
      • 2014-06-04
      • 1970-01-01
      • 2013-11-06
      相关资源
      最近更新 更多