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

使用pnpm时@discordjs/voice无法检测已安装的加密包

使用@discordjs/voice播放音频时依赖检测失败问题(pnpm环境)

已在项目中安装@discordjs/opus、libsodium-wrappers、sodium-native、tweetnacl等依赖,但@discordjs/voice无法检测到,报错提示缺少加密包和Opus库,推测是pnpm的node_modules结构导致。

错误日志

ERROR  file:///C:/Users/[username]/Desktop/code/[project name]/node_modules/.pnpm/@discordjs+voice@0.13.0_@discordjs+opus@0.9.0/node_modules/@discordjs/voice/src/util/Secretbox.ts:51
        throw new Error(
       ^


 ERROR  Error: Cannot play audio as no valid encryption package is installed.                                 20:03:27
- Install sodium, libsodium-wrappers, or tweetnacl.
- Use the generateDependencyReport() function for more information.

    at Object.fallbackError (file:///C:/Users/[username]/Desktop/code/[project name]/node_modules/.pnpm/@discordjs+voice@0.13.0_@discordjs+opus@0.9.0/node_modules/@discordjs/voice/src/util/Secretbox.ts:51:8)
    at Networking.encryptOpusPacket (file:///C:/Users/[username]/Desktop/code/[project name]/node_modules/.pnpm/@discordjs+voice@0.13.0_@discordjs+opus@0.9.0/node_modules/@discordjs/voice/src/networking/Networking.ts:585:23)
    at Networking.createAudioPacket (file:///C:/Users/[username]/Desktop/code/[project name]/node_modules/.pnpm/@discordjs+voice@0.13.0_@discordjs+opus@0.9.0/node_modules/@discordjs/voice/src/networking/Networking.ts:568:47)
    at Networking.prepareAudioPacket (file:///C:/Users/[username]/Desktop/code/[project name]/node_modules/.pnpm/@discordjs+voice@0.13.0_@discordjs+opus@0.9.0/node_modules/@discordjs/voice/src/networking/Networking.ts:490:31)
    at VoiceConnection.prepareAudioPacket (file:///C:/Users/[username]/Desktop/code/[project name]/node_modules/.pnpm/@discordjs+voice@0.13.0_@discordjs+opus@0.9.0/node_modules/@discordjs/voice/src/VoiceConnection.ts:520:27)
    at AudioPlayer._preparePacket (file:///C:/Users/[username]/Desktop/code/[project name]/node_modules/.pnpm/@discordjs+voice@0.13.0_@discordjs+opus@0.9.0/node_modules/@discordjs/voice/src/audio/AudioPlayer.ts:640:15)
    at AudioPlayer._stepPrepare (file:///C:/Users/[username]/Desktop/code/[project name]/node_modules/.pnpm/@discordjs+voice@0.13.0_@discordjs+opus@0.9.0/node_modules/@discordjs/voice/src/audio/AudioPlayer.ts:604:10)
    at prepareNextAudioFrame (file:///C:/Users/[username]/Desktop/code/[project name]/node_modules/.pnpm/@discordjs+voice@0.13.0_@discordjs+opus@0.9.0/node_modules/@discordjs/voice/src/DataStore.ts:146:13)
    at audioCycleStep (file:///C:/Users/[username]/Desktop/code/[project name]/node_modules/.pnpm/@discordjs+voice@0.13.0_@discordjs+opus@0.9.0/node_modules/@discordjs/voice/src/DataStore.ts:127:2)
    at Timeout._onTimeout (file:///C:/Users/[username]/Desktop/code/[project name]/node_modules/.pnpm/@discordjs+voice@0.13.0_@discordjs+opus@0.9.0/node_modules/@discordjs/voice/src/DataStore.ts:139:42)

依赖检测报告

--------------------------------------------------                                                            20:11:50
Core Dependencies
- @discordjs/voice: 0.13.0
- prism-media: not found

Opus Libraries
- @discordjs/opus: not found
- opusscript: not found

Encryption Libraries
- sodium-native: not found
- sodium: not found
- libsodium-wrappers: not found
- tweetnacl: not found

FFmpeg
- version: 2021-09-08-git-5e7e2e5031-full_build-www.gyan.dev
- libopus: yes
--------------------------------------------------

项目依赖配置

"dependencies": {
    "@discordjs/opus": "^0.9.0",
    "@discordjs/voice": "^0.13.0",
    "consola": "^2.15.3",
    "discord.js": "^14.6.0",
    "libsodium-wrappers": "^0.7.10",
    "sodium-native": "^3.4.1",
    "tweetnacl": "^1.0.3"
},

解决方法

1. 启用pnpm的shamefully-hoist选项

pnpm默认隔离依赖,导致@discordjs/voice无法访问同级安装的加密库和Opus库。启用shamefully-hoist可将依赖提升至根node_modules:

  • 在项目根目录创建.npmrc文件,添加:
    shamefully-hoist=true
    
  • 清理并重新安装依赖:
    pnpm install --shamefully-hoist
    
    或临时用命令行参数启用:
    pnpm install --shamefully-hoist
    

2. 手动声明peer依赖元数据

在package.json中添加peerDependenciesMeta,强制pnpm将指定依赖暴露给@discordjs/voice:

"peerDependenciesMeta": {
  "@discordjs/opus": { "optional": true },
  "libsodium-wrappers": { "optional": true },
  "tweetnacl": { "optional": true }
}

执行pnpm install生效。

3. 清理缓存后重装

若上述方法无效,清理pnpm缓存后重新安装:

pnpm store prune
pnpm install

4. 降级@discordjs/voice版本(可选)

部分旧版本对pnpm兼容性更好,可尝试降级至0.12.x版本,同时调整对应依赖版本匹配。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 07:25:21