Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get ReferenceError: RTCPeerConnection is not defined in Next.js?

I am trying to const pc = new RTCPeerConnection() while I get ReferenceError: RTCPeerConnection is not defined. How can I overcome this error?

It is not my browsers, I can run webRTC natively on them.

like image 551
Hypothesis Avatar asked Sep 20 '25 11:09

Hypothesis


1 Answers

Next.js pre-renders every page on the server. Trying to use Web APIs when the page gets pre-rendered will throw an error like the one you're seeing since those Web APIs are not present in a Node.js environment.

To solve it, make sure you call new RTCPeerConnection() within your component's useEffect so it only gets called on the client-side.

useEffect(() => {
    const pc = new RTCPeerConnection()
    // Rest of your logic here
}, [])
like image 179
juliomalves Avatar answered Sep 23 '25 02:09

juliomalves