【问题标题】:Next.js yarn build failed due to plotly.js (Build error occurred ReferenceError: self is not defined)Next.js 纱线构建因 plotly.js 失败(发生构建错误 ReferenceError: self is not defined)
【发布时间】:2021-12-02 02:35:33
【问题描述】:

当我尝试使用 yarn build 命令构建用于生产的 Next.js 应用程序时出现以下错误。但它在开发服务器中运行良好(使用yarn dev 命令时)。我正在使用 react-plotly.js 库在此反应应用程序中生成绘图图。

react-plotly 库的使用:

import Plot from 'react-plotly.js';

return(
  <>
   <Plot data={[data]} layout={layout} />
  </>
)

这是错误:

> Build error occurred
ReferenceError: self is not defined

我已经研究了 plotly.js git 存储库和谷歌类似的问题,但似乎没有结果。知道 Next.js 纱线构建失败的原因吗?

【问题讨论】:

    标签: javascript reactjs next.js build-error plotly.js


    【解决方案1】:

    Next Js 使用服务器端渲染,并且在本地开发时仍然模拟 ssr,因此许多依赖于原生对象(通常是窗口对象)的库无法按预期工作。

    https://github.com/plotly/react-plotly.js/issues/40

    似乎 plotly 不适用于 ssr 但还有另一个组件应该 https://github.com/aulneau/next.js-with-react-plotly.js

    【讨论】:

    • 感谢您的解决方案。没有 SSR 的动态导入对我有用。你节省了我的时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 2021-09-14
    • 1970-01-01
    • 2022-09-27
    • 2019-03-07
    • 1970-01-01
    • 2021-10-10
    相关资源
    最近更新 更多