You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NestJS无法使用@WebSocketGateway装饰器,报metadataScanner相关TypeError

解决NestJS中@WebSocketGateway报错:TypeError: this.metadataScanner.getAllMethodNames is not a function

问题根源

这个错误确实是版本不兼容导致的:@nestjs/websockets 包中的 GatewayMetadataExplorer 类依赖 @nestjs/core 提供的 metadataScanner.getAllMethodNames 方法,但两者版本不匹配时,要么旧版 @nestjs/core 没有这个方法,要么新版 websockets 对该方法的调用逻辑和旧版 core 不兼容。

解决方案

  • 统一NestJS核心包版本
    确保 @nestjs/core、@nestjs/websockets 以及关联的 socket 平台包(比如 @nestjs/platform-socket.io)使用完全一致的版本。修改 package.json 中的依赖项,示例如下:

    {
      "dependencies": {
        "@nestjs/core": "^10.3.0",
        "@nestjs/websockets": "^10.3.0",
        "@nestjs/platform-socket.io": "^10.3.0"
      }
    }
    

    可以选择项目中已有的核心版本,或者直接使用最新稳定版。

  • 清理并重新安装依赖
    执行以下命令清除缓存和旧依赖,重新安装确保版本一致性:

    rm -rf node_modules package-lock.json
    npm install
    

    如果你用 yarn,替换为对应的命令:

    rm -rf node_modules yarn.lock
    yarn install
    
  • 排查第三方包版本冲突
    部分第三方插件或自定义装饰器包可能依赖旧版 @nestjs/core,导致版本冲突。可以通过以下命令查看依赖树,定位冲突包:

    npm ls @nestjs/core
    

    找到冲突的包后,要么更新该包到兼容当前NestJS版本的版本,要么替换为替代方案。

内容的提问来源于stack exchange,提问作者Muhammad Umar Abdullah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 05:12:42