-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"ReferenceError: document is not defined" in Next.js13 & 14 #1119
Comments
Same issue here. No idea what changed, please let me know if there's a package update that broke Plyr... |
@maxiedaniels @eliyo @Clueed please add more question and details also there |
I don't know if this is an error due to nextjs version or your source code. But this error appears in nextjs 14.0.2 and above, but 14.0.1 still works normally. |
The error disappeared for me, no idea why. I had already tried deleting node_modules and my package lock file, but it hadn’t fixed it. I gave up and then somehow a day later I realized the error had disappeared. |
Also getting this error but with Remix |
same, got this error with nextjs 14.0.2 |
|
@timomedia it allows me to run npm run dev if I recall but I can’t do an actual build or deploy on vercel |
Hello,
it should work ;) |
This works to render, but it doesn't seem possible to get the ref to to the component. If you just attach the All my attempts beyond this to dynamically import usePlyr and do React.forwardRef() have failed, as well as all attempts to do a basic forwarded-ref component. Currently stuck with not being able to use this library, which is a bummer. |
I was getting this problem too but I now resolved it (I think). Here's an example component if it helps anyone out there:
Interestingly, if I add |
The dynamic load fixed it for me (const Plyr = dynamic(() => import("plyr-react"), { ssr: false });) const PlyrComponent = React.useMemo(() => { And then <PlyrComponent ...> instead of <Plyr ...> Just sharing in case someone else has the same bug! |
@maxiedaniels @michaelpumo
|
A workaround could be to create a wrapper component around the Plyr component that uses a callback ref to provide access to the underlying Plyr instance. Here's an example:
In this code, PlyrWrapper is a component that wraps the Plyr component. It uses a callback ref (plyrRef) to get a reference to the Plyr component, and then assigns the Plyr instance (plyrRef.current.plyr) to the forwarded ref (ref). You can then use this PlyrWrapper component like this:
This should allow you to get a ref to the Plyr instance. |
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:
The text was updated successfully, but these errors were encountered: