react-icons module causing next.js ReferenceError: document is not defined
react-icons module causing next.js ReferenceError: document is not defined
正如标题所说,我尝试使用 react-icons 模块,但它导致引用错误。
我使用命令 yarn add react-icons
安装了模块。
详情如下
wait - compiling /_error (client and server)...
event - compiled client and server successfully in 181 ms (249 modules)
ReferenceError: document is not defined
at new StyleSheet (/node_modules/styled-jsx/dist/index/index.js:136:35)
at new StyleSheetRegistry (pathToProject/node_modules/styled-jsx/dist/index/index.js:491:33)
at Object.createStyleRegistry (pathToProject/node_modules/styled-jsx/dist/index/index.js:667:10)
at Object.renderToHTML (pathToProject/node_modules/next/dist/server/render.js:364:46)
at async doRender (pathToProject/node_modules/next/dist/server/base-server.js:879:38)
at async cacheEntry.responseCache.get.isManualRevalidate.isManualRevalidate (pathToProject/node_modules/next/dist/server/base-server.js:979:28)
at async pathToProject/node_modules/next/dist/server/response-cache.js:64:36
ReferenceError: document is not defined
at new StyleSheet (pathToProject/node_modules/styled-jsx/dist/index/index.js:136:35)
at new StyleSheetRegistry (pathToProject/node_modules/styled-jsx/dist/index/index.js:491:33)
at Object.createStyleRegistry (pathToProject/node_modules/styled-jsx/dist/index/index.js:667:10)
at Object.renderToHTML (pathToProject/node_modules/next/dist/server/render.js:364:46)
at async doRender (pathToProject/node_modules/next/dist/server/base-server.js:879:38)
at async cacheEntry.responseCache.get.isManualRevalidate.isManualRevalidate (pathToProject/node_modules/next/dist/server/base-server.js:979:28)
at async pathToProject/node_modules/next/dist/server/response-cache.js:64:36
ReferenceError: document is not defined
at new StyleSheet (pathToProject/node_modules/styled-jsx/dist/index/index.js:136:35)
at new StyleSheetRegistry (pathToProject/node_modules/styled-jsx/dist/index/index.js:491:33)
at Object.createStyleRegistry (pathToProject/node_modules/styled-jsx/dist/index/index.js:667:10)
at Object.renderToHTML (pathToProject/node_modules/next/dist/server/render.js:364:46)
at async doRender (pathToProject/node_modules/next/dist/server/base-server.js:879:38)
at async cacheEntry.responseCache.get.isManualRevalidate.isManualRevalidate (pathToProject/node_modules/next/dist/server/base-server.js:979:28)
at async pathToProject/node_modules/next/dist/server/response-cache.js:64:36
ReferenceError: document is not defined
at new StyleSheet (pathToProject/node_modules/styled-jsx/dist/index/index.js:136:35)
at new StyleSheetRegistry (pathToProject/node_modules/styled-jsx/dist/index/index.js:491:33)
at Object.createStyleRegistry (pathToProject/node_modules/styled-jsx/dist/index/index.js:667:10)
at Object.renderToHTML (pathToProject/node_modules/next/dist/server/render.js:364:46)
at async doRender (pathToProject/node_modules/next/dist/server/base-server.js:879:38)
at async cacheEntry.responseCache.get.isManualRevalidate.isManualRevalidate (pathToProject/node_modules/next/dist/server/base-server.js:979:28)
at async pathToProject/node_modules/next/dist/server/response-cache.js:64:36
ReferenceError: document is not defined
at new StyleSheet (pathToProject/node_modules/styled-jsx/dist/index/index.js:136:35)
at new StyleSheetRegistry (pathToProject/node_modules/styled-jsx/dist/index/index.js:491:33)
at Object.createStyleRegistry (pathToProject/node_modules/styled-jsx/dist/index/index.js:667:10)
at Object.renderToHTML (pathToProject/node_modules/next/dist/server/render.js:364:46)
at async doRender (pathToProject/node_modules/next/dist/server/base-server.js:879:38)
at async cacheEntry.responseCache.get.isManualRevalidate.isManualRevalidate (pathToProject/node_modules/next/dist/server/base-server.js:979:28)
at async pathToProject/node_modules/next/dist/server/response-cache.js:64:36
ReferenceError: document is not defined
at new StyleSheet (pathToProject/node_modules/styled-jsx/dist/index/index.js:136:35)
at new StyleSheetRegistry (pathToProject/node_modules/styled-jsx/dist/index/index.js:491:33)
at Object.createStyleRegistry (pathToProject/node_modules/styled-jsx/dist/index/index.js:667:10)
at Object.renderToHTML (pathToProject/node_modules/next/dist/server/render.js:364:46)
at async doRender (pathToProject/node_modules/next/dist/server/base-server.js:879:38)
at async cacheEntry.responseCache.get.isManualRevalidate.isManualRevalidate (pathToProject/node_modules/next/dist/server/base-server.js:979:28)
at async pathToProject/node_modules/next/dist/server/response-cache.js:64:36
error - unhandledRejection: ReferenceError: document is not defined
error - unhandledRejection: ReferenceError: document is not defined
error - unhandledRejection: ReferenceError: document is not defined
error - unhandledRejection: ReferenceError: document is not defined
error - unhandledRejection: ReferenceError: document is not defined
error - unhandledRejection: ReferenceError: document is not defined
通过禁用服务器端渲染解决了这个问题。
import dynamic from "next/dynamic"
const SetupMavlink = dynamic(() => import("components/SetupMavlink"), {ssr:false})
export default function Home() {
return (
<div >
<SetupMavlink />
</div>
)
}
正如标题所说,我尝试使用 react-icons 模块,但它导致引用错误。
我使用命令 yarn add react-icons
安装了模块。
详情如下
wait - compiling /_error (client and server)...
event - compiled client and server successfully in 181 ms (249 modules)
ReferenceError: document is not defined
at new StyleSheet (/node_modules/styled-jsx/dist/index/index.js:136:35)
at new StyleSheetRegistry (pathToProject/node_modules/styled-jsx/dist/index/index.js:491:33)
at Object.createStyleRegistry (pathToProject/node_modules/styled-jsx/dist/index/index.js:667:10)
at Object.renderToHTML (pathToProject/node_modules/next/dist/server/render.js:364:46)
at async doRender (pathToProject/node_modules/next/dist/server/base-server.js:879:38)
at async cacheEntry.responseCache.get.isManualRevalidate.isManualRevalidate (pathToProject/node_modules/next/dist/server/base-server.js:979:28)
at async pathToProject/node_modules/next/dist/server/response-cache.js:64:36
ReferenceError: document is not defined
at new StyleSheet (pathToProject/node_modules/styled-jsx/dist/index/index.js:136:35)
at new StyleSheetRegistry (pathToProject/node_modules/styled-jsx/dist/index/index.js:491:33)
at Object.createStyleRegistry (pathToProject/node_modules/styled-jsx/dist/index/index.js:667:10)
at Object.renderToHTML (pathToProject/node_modules/next/dist/server/render.js:364:46)
at async doRender (pathToProject/node_modules/next/dist/server/base-server.js:879:38)
at async cacheEntry.responseCache.get.isManualRevalidate.isManualRevalidate (pathToProject/node_modules/next/dist/server/base-server.js:979:28)
at async pathToProject/node_modules/next/dist/server/response-cache.js:64:36
ReferenceError: document is not defined
at new StyleSheet (pathToProject/node_modules/styled-jsx/dist/index/index.js:136:35)
at new StyleSheetRegistry (pathToProject/node_modules/styled-jsx/dist/index/index.js:491:33)
at Object.createStyleRegistry (pathToProject/node_modules/styled-jsx/dist/index/index.js:667:10)
at Object.renderToHTML (pathToProject/node_modules/next/dist/server/render.js:364:46)
at async doRender (pathToProject/node_modules/next/dist/server/base-server.js:879:38)
at async cacheEntry.responseCache.get.isManualRevalidate.isManualRevalidate (pathToProject/node_modules/next/dist/server/base-server.js:979:28)
at async pathToProject/node_modules/next/dist/server/response-cache.js:64:36
ReferenceError: document is not defined
at new StyleSheet (pathToProject/node_modules/styled-jsx/dist/index/index.js:136:35)
at new StyleSheetRegistry (pathToProject/node_modules/styled-jsx/dist/index/index.js:491:33)
at Object.createStyleRegistry (pathToProject/node_modules/styled-jsx/dist/index/index.js:667:10)
at Object.renderToHTML (pathToProject/node_modules/next/dist/server/render.js:364:46)
at async doRender (pathToProject/node_modules/next/dist/server/base-server.js:879:38)
at async cacheEntry.responseCache.get.isManualRevalidate.isManualRevalidate (pathToProject/node_modules/next/dist/server/base-server.js:979:28)
at async pathToProject/node_modules/next/dist/server/response-cache.js:64:36
ReferenceError: document is not defined
at new StyleSheet (pathToProject/node_modules/styled-jsx/dist/index/index.js:136:35)
at new StyleSheetRegistry (pathToProject/node_modules/styled-jsx/dist/index/index.js:491:33)
at Object.createStyleRegistry (pathToProject/node_modules/styled-jsx/dist/index/index.js:667:10)
at Object.renderToHTML (pathToProject/node_modules/next/dist/server/render.js:364:46)
at async doRender (pathToProject/node_modules/next/dist/server/base-server.js:879:38)
at async cacheEntry.responseCache.get.isManualRevalidate.isManualRevalidate (pathToProject/node_modules/next/dist/server/base-server.js:979:28)
at async pathToProject/node_modules/next/dist/server/response-cache.js:64:36
ReferenceError: document is not defined
at new StyleSheet (pathToProject/node_modules/styled-jsx/dist/index/index.js:136:35)
at new StyleSheetRegistry (pathToProject/node_modules/styled-jsx/dist/index/index.js:491:33)
at Object.createStyleRegistry (pathToProject/node_modules/styled-jsx/dist/index/index.js:667:10)
at Object.renderToHTML (pathToProject/node_modules/next/dist/server/render.js:364:46)
at async doRender (pathToProject/node_modules/next/dist/server/base-server.js:879:38)
at async cacheEntry.responseCache.get.isManualRevalidate.isManualRevalidate (pathToProject/node_modules/next/dist/server/base-server.js:979:28)
at async pathToProject/node_modules/next/dist/server/response-cache.js:64:36
error - unhandledRejection: ReferenceError: document is not defined
error - unhandledRejection: ReferenceError: document is not defined
error - unhandledRejection: ReferenceError: document is not defined
error - unhandledRejection: ReferenceError: document is not defined
error - unhandledRejection: ReferenceError: document is not defined
error - unhandledRejection: ReferenceError: document is not defined
通过禁用服务器端渲染解决了这个问题。
import dynamic from "next/dynamic"
const SetupMavlink = dynamic(() => import("components/SetupMavlink"), {ssr:false})
export default function Home() {
return (
<div >
<SetupMavlink />
</div>
)
}