首页 > 解决方案 > ReferenceError:未定义 AudioBuffer

问题描述

我正在尝试在 next.js 反应项目中使用tone.js。当我运行或构建时,我收到此错误“ReferenceError: AudioBuffer is not defined”

我已经在代码和框中隔离了tone.js 代码,它运行良好。 https://codesandbox.io/s/tonejs-react-pink-noise-generator-pfnl0?file=/src/App.js

但不在 next.js 应用程序中

ReferenceError: AudioBuffer is not defined
    at ki (/node_modules/tone/build/Tone.js:1:117922)```

标签: next.jstone.js

解决方案


AudioBuffer是 Web 浏览器的东西,它在 node.js 中不存在。您收到的错误是因为使用的代码AudioBuffer无法在服务器上运行。您只需要在浏览器中实例化这部分代码。这通常在useEffect钩子中完成,或者在服务器上,您可以首先检查window对象是否已定义。


推荐阅读