petercat-ai / petercat

A conversational Q&A agent configuration system, self-hosted deployment solutions, and a convenient all-in-one application SDK, allowing you to create intelligent Q&A bots for your GitHub repositories
https://petercat.ai
MIT License
595 stars 16 forks source link

When `yarn run client`, occupy ReferenceError: document is not defined #464

Open AntiTopQuark opened 1 week ago

AntiTopQuark commented 1 week ago

Describe the bug When yarn run client, occupy ReferenceError

 yarn run client
yarn run v1.22.22
$ cd client && yarn run dev
$ next dev
   ▲ Next.js 14.0.1
   - Local:        http://localhost:3000
   - Environments: .env
   - Experiments (use at your own risk):
     · instrumentationHook

 ○ Compiling /instrumentation ...
 ✓ Compiled /instrumentation in 730ms (219 modules)
 ✓ Ready in 2.9s
 ○ Compiling /page ...
<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
<w> [webpack.cache.PackFileCacheStrategy] Caching failed for pack: Error: Unable to snapshot resolve dependencies
 ✓ Compiled /page in 35.3s (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
 ⨯ 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 2s (11312 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
 ⨯ 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

To 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. image image

petercat-assistant[bot] commented 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).

Recommendations to Resolve the Issue:

  1. 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.

  2. 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 });
  3. Check for window or document: Before using document, check if it is defined:

    if (typeof document !== 'undefined') {
       // Your code that uses document
    }
  4. 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.

  5. Review Documentation: Double-check the documentation for lottie-web and Next.js to ensure compatibility and best practices for SSR.

Additional Context:

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.

AntiTopQuark commented 1 week ago

This question is inexplicably good.

petercat-assistant[bot] commented 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 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.

Steps to Resolve:

  1. 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.

  2. 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 });
  3. 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
    }
  4. 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.

  5. 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.

Additional Context:

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.

AntiTopQuark commented 1 week ago

I can enter the main page, but the market page is still not open. image

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
xingwanying commented 1 week ago

You need to run server first