I'm trying to use tone.js in a next.js react project. When I run or build i get this error "ReferenceError: AudioBuffer is not defined"
I have isolated the tone.js code in codesandbox and it's working perfectly. https://codesandbox.io/s/tonejs-react-pink-noise-generator-pfnl0?file=/src/App.js
But not in the next.js app
ReferenceError: AudioBuffer is not defined
at ki (/node_modules/tone/build/Tone.js:1:117922)```
AudioBuffer is a web browser thing, it does not exist in node.js. The error you are getting is because the code that uses
AudioBuffer
can't run on the server. You need to instantiate that part of the code only in the browser. That is usually done inuseEffect
hook, or on the server, you can first check ifwindow
object is defined.