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

@solana/web3.js报错:无法找到rpc-websockets/dist/lib/client模块求解决

解决@solana/web3.js导入Connection时找不到rpc-websockets模块的问题

问题场景

项目依赖@raydium-io/raydium-sdk@^1.3.1-beta.52,依赖链如下:

`-- @project-serum/serum@0.13.65
  `-- @solana/web3.js@1.91.8
    `-- rpc-websockets@7.11.1

执行import { Connection } from '@solana/web3.js'并运行项目时,出现以下错误:

[INFO] 20:49:10 ts-node-dev ver. 2.0.0 (using ts-node ver. 10.9.2, typescript ver. 5.4.5)
Error: Cannot find module 'rpc-websockets/dist/lib/client'

已确认rpc-websockets存在于node_modules中,推测是模块解析问题,但不确定具体原因。

解决方案

  • 清理并重新安装依赖
    删除依赖目录和锁文件,重新安装确保依赖完整:

    rm -rf node_modules package-lock.json yarn.lock
    npm install # 或 yarn install
    

    这能解决缓存或安装过程中导致的文件缺失、版本不匹配问题。

  • 调整TypeScript模块解析配置
    打开tsconfig.json,确保moduleResolution与项目模块系统匹配:

    {
      "compilerOptions": {
        "moduleResolution": "node16", // 或"node",根据项目使用ES模块/CommonJS调整
        "module": "ESNext" // 对应ES模块,使用CommonJS则设为"CommonJS"
      }
    }
    

    旧的解析模式可能无法正确识别嵌套依赖的文件路径。

  • 直接锁定rpc-websockets版本
    在package.json的dependencies中手动添加指定版本,强制项目使用正确的包版本:

    {
      "dependencies": {
        "rpc-websockets": "7.11.1"
      }
    }
    

    添加后重新执行安装命令,确保依赖树中版本统一。

  • 适配ts-node-dev的模块系统
    如果项目使用ES模块,先在package.json中添加:

    "type": "module"
    

    然后启动时添加--esm参数:

    ts-node-dev --esm your-entry-file.ts
    

    避免模块系统不兼容导致的解析失败。

  • 验证并修复rpc-websockets包完整性
    进入node_modules/rpc-websockets目录,检查dist/lib/client.js(或对应TypeScript文件)是否存在。如果缺失,强制重新安装该包:

    npm install rpc-websockets@7.11.1 --force
    

内容的提问来源于stack exchange,提问作者salat _

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 05:16:13