Open AntiTopQuark opened 1 week ago
The error you're encountering, ReferenceError: document is not defined
, typically occurs when server-side code attempts to access the document
object, which is only available in a browser environment. This is a common issue when using libraries like lottie-web
that are designed to run in the browser but are being executed in a server-side context, such as with Next.js server-side rendering (SSR).
Conditional Import: Ensure that the lottie-web
library is only imported and used in the client-side code. You can achieve this by using dynamic imports or conditional checks to ensure the code runs only in the browser.
Dynamic Import: Use Next.js's dynamic import functionality to load the component only on the client-side:
import dynamic from 'next/dynamic';
const LottieComponent = dynamic(() => import('path-to-your-lottie-component'), { ssr: false });
Check for window
or document
: Before using document
, check if it is defined:
if (typeof document !== 'undefined') {
// Your code that uses document
}
Update the Component: If you have control over the component using lottie-web
, ensure it checks for the existence of document
before executing any code that relies on it.
Review Documentation: Double-check the documentation for lottie-web
and Next.js to ensure compatibility and best practices for SSR.
boto
client and the fake region name might not directly impact this issue, but it's worth ensuring that all environment configurations are correctly set up to avoid any side effects.If these suggestions do not resolve the issue, please provide more details about the specific usage of lottie-web
in your codebase, and I can assist further.
For further assistance, please describe your question in the comments and @petercat-assistant to start a conversation with me.
This question is inexplicably good.
The error you're encountering, "ReferenceError: document is not defined," typically occurs when server-side code attempts to access the document
object, which is only available in the browser environment. This is a common issue when using libraries that are not compatible with server-side rendering (SSR) in frameworks like Next.js.
Check for Client-Side Only Code:
Ensure that any code that relies on the document
object is only executed on the client side. You can use useEffect
in React to run code after the component has mounted, which ensures it runs only in the browser.
Dynamic Import:
Use Next.js's dynamic import feature to load components or libraries only on the client side. This can be done by setting the ssr
option to false
.
Example:
import dynamic from 'next/dynamic';
const LottiePlayer = dynamic(() => import('lottie-web'), { ssr: false });
Conditional Checks:
Add conditional checks to ensure that code accessing document
only runs in the browser environment.
Example:
if (typeof window !== 'undefined') {
// Code that uses document
}
Library Compatibility:
Verify if the lottie-web
library has specific instructions or configurations for use with SSR. Sometimes, libraries provide alternative methods or configurations for SSR compatibility.
Review the Code:
Check the specific lines in your code where the error is thrown (e.g., ./app/page.tsx:14:70
) and ensure that any DOM manipulations or references to document
are appropriately handled.
The Python error you mentioned regarding the boto
client and a fake region name should not directly impact the JavaScript error you're encountering. However, ensure that all configurations and environment variables are correctly set, as they might affect other parts of your application.
For further assistance, please describe your question in the comments and @petercat-assistant to start a conversation with me.
I can enter the main page, but the market page is still not open.
There are some changes of error log
✓ Compiled /page in 37.2s (23484 modules)
⨯ node_modules/lottie-web/build/player/lottie.js (30:0) @ createTag
⨯ ReferenceError: document is not defined
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at eval (./app/page.tsx:14:70)
at (ssr)/./app/page.tsx (/home/antitopquark/project/petercat/client/.next/server/app/page.js:520:1)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at JSON.parse (<anonymous>)
null
⨯ node_modules/lottie-web/build/player/lottie.js (30:0) @ createTag
⨯ ReferenceError: document is not defined
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at eval (./app/page.tsx:14:70)
at (ssr)/./app/page.tsx (/home/antitopquark/project/petercat/client/.next/server/app/page.js:520:1)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at JSON.parse (<anonymous>)
null
✓ Compiled in 2.3s (11312 modules)
✓ Compiled in 470ms (11298 modules)
<w> [webpack.cache.PackFileCacheStrategy] Caching failed for pack: Error: Unable to snapshot resolve dependencies
<w> [webpack.cache.PackFileCacheStrategy] Caching failed for pack: Error: Unable to snapshot resolve dependencies
○ Compiling /market/page ...
✓ Compiled /market/page in 4.7s (23439 modules)
⨯ node_modules/lottie-web/build/player/lottie.js (30:0) @ createTag
⨯ ReferenceError: document is not defined
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at eval (./app/market/page.tsx:17:80)
at (ssr)/./app/market/page.tsx (/home/antitopquark/project/petercat/client/.next/server/app/market/page.js:531:1)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at JSON.parse (<anonymous>)
null
⚠ Fast Refresh had to perform a full reload. Read more: https://nextjs.org/docs/messages/fast-refresh-reload
⨯ node_modules/lottie-web/build/player/lottie.js (30:0) @ createTag
⨯ ReferenceError: document is not defined
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at eval (./app/market/page.tsx:17:80)
at (ssr)/./app/market/page.tsx (/home/antitopquark/project/petercat/client/.next/server/app/market/page.js:531:1)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at JSON.parse (<anonymous>)
null
⨯ node_modules/lottie-web/build/player/lottie.js (30:0) @ createTag
⨯ ReferenceError: document is not defined
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at eval (./app/market/page.tsx:17:80)
at (ssr)/./app/market/page.tsx (/home/antitopquark/project/petercat/client/.next/server/app/market/page.js:531:1)
at __webpack_require__ (/home/antitopquark/project/petercat/client/.next/server/webpack-runtime.js:33:43)
at JSON.parse (<anonymous>)
null
You need to run server first
Describe the bug When
yarn run client
, occupy ReferenceErrorTo Reproduce According to this doc : https://github.com/petercat-ai/petercat/blob/main/docs/guides/self_hosted_local_cn.md
Expected behavior Open Client's web page
Screenshots
Desktop (please complete the following information):
Additional context I encountered a Python error when executing yarn run server. I set a fake region name for the boto client. I don't know if it will have any impact.