Skip to content

"ReferenceError: document is not defined" in Next.js13 & 14 #1119

@eliyo

Description

@eliyo

I am using plyr-react in a Next.js 14.0.1 environment (also tried with v13) and pages using plyr-react (5.3.0) fail to load or build with errors of type "ReferenceError: document is not defined" node_modules\plyr\dist\plyr.js:302:21

From my understanding, it seems the runtime tries to access the document object where there isn't one, like when running in a server environment. These errors can be fixed by checking validity of the document object before accessing it, but I don't think this is a proper fix, it seems the code is being run within a context it shouldn't be run into.

Windows 10, Node.js 21.1.0, Next.js 14.0.1, plyr-react 5.3.0, react 18.2.0

FYI I didn't have this problem when I worked on my site about 6 months ago, but now I upgraded everything I have it all the time, this blocks the build.

Steps to reproduce:

  1. install latest next.js and example app using: npx create-next-app@latest
  2. npm install plyr-react
  3. into the app's src/app/page.tsx, import plyr and and a tag in the Home function.
import Plyr from "plyr-react"
import "plyr-react/plyr.css"
(...)
<Plyr (...) />
  1. npm run dev
  2. open page in browser and see the error "ReferenceError: document is not defined"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions