lottie-react icon indicating copy to clipboard operation
lottie-react copied to clipboard

ReferenceError: document is not defined at createTag (node >18)

Open zifeo opened this issue 2 years ago • 15 comments

Overview

Using NextJS, we got:

Error occurred prerendering page "/X". Read more: https://nextjs.org/docs/messages/prerender-error
ReferenceError: document is not defined at createTag ...

This has been reported as a potential bug related to the node version: https://github.com/Gamote/lottie-react/issues/101.

Consuming repo

What repo were you working in when this issue occurred?

# use node 20 or 21
npx create-next-app@latest
# edit app/page.ts
# add the Player component as demo

Labels

  • [ ] Add the Type: Bug label to this issue.

zifeo avatar Nov 30 '23 11:11 zifeo

I'm getting the same error as well on latest node version. Switching to 18.18.2 fixed it for me.

no-complect avatar Nov 30 '23 18:11 no-complect

im getting this as well on node v20+

Screenshot 2024-01-01 at 6 09 49 PM

sonangrai avatar Jan 01 '24 12:01 sonangrai

Please 'use client' in component using this library.

[Nextjs] For me, I also separate animation lottie into single component with 'use client' in the top of file: image

And import this to Server component by dynamic import: image

dzungmv avatar Jan 05 '24 03:01 dzungmv

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs.

github-actions[bot] avatar Mar 06 '24 01:03 github-actions[bot]

This impacts more than just Next. I'm using this repo in a gatsby project am am running into the same issue. Getting the error: ReferenceError: document is not defined

edwardoverthere avatar May 14 '24 22:05 edwardoverthere

I confirm this error is gone if you dynamically import the Player component as @dzungmv suggested.

This is my solution.

// player.tsx
import dynamic from 'next/dynamic';

export const ReactLottiePlayer = dynamic(
  async () => {
    const ReactLottie = await import('@lottiefiles/react-lottie-player');
    return ReactLottie.Player;
  },
  { ssr: false }
);
// page.tsx
'use client';

import { ReactLottiePlayer } from '@/lib/lottiefiles/player';

export function Page() {
  return (
    <ReactLottiePlayer
      src="/animation.json"
      speed={1}
      loop
      autoplay
    />
  ); 
}

Tested in Next.js v14.x

adrivelasco avatar Jul 02 '24 17:07 adrivelasco

My Project was complex so dynamic import was not feasible solution. It works fine on node v18 though

M-YasirGhaffar avatar Jul 07 '24 20:07 M-YasirGhaffar

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs.

github-actions[bot] avatar Sep 06 '24 01:09 github-actions[bot]

downgraded from node22 to node20

fl0wo avatar Nov 12 '24 19:11 fl0wo

is there any solution for node v22? I cannot use lazy loading..

SapirBar2 avatar Nov 19 '24 12:11 SapirBar2

Any update on this issue, I also can't use lazy loading?

joeykamsteeg avatar Dec 10 '24 16:12 joeykamsteeg

It looks like this version of the player is no longer actively maintained. After encountering the same issue, I upgraded to the newer version of the player @lottiefiles/dotlottie-react , as mentioned in the current LottieFiles documentation, and everything works fine now. Sadly it's not completely 1:1, so transition requires little bit of tweaking.

JosefDuda avatar Dec 17 '24 17:12 JosefDuda

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs.

github-actions[bot] avatar Feb 16 '25 02:02 github-actions[bot]

We should run node with --no-experimental-global-navigator option. Refs

  • https://github.com/airbnb/lottie-web/issues/3047
  • https://nodejs.org/docs/latest-v22.x/api/cli.html#--no-experimental-global-navigator
  • https://github.com/nodejs/node/issues/39540

leeeunsang-tmobi avatar Mar 11 '25 09:03 leeeunsang-tmobi

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs.

github-actions[bot] avatar May 11 '25 02:05 github-actions[bot]