reactjs - Gatsby + React 预订日历:“WebpackError: ReferenceError: self is not defined” on 'gatsby build' 但 'gatsby develop' 工作正常
问题描述
我正在使用 Gatsby 建立一个网站。我添加了React Booking Calendar来显示一些日历数据。
运行 'gatsby develop' 工作正常,但运行 'gatsby build' 会产生以下错误:
WebpackError: ReferenceError: self is not defined
- index.js:13
node_modules/react-booking-calendar/lib/index.js:13:10310
- index.js:13
node_modules/react-booking-calendar/lib/index.js:13:10234
- index.js:13 e.exports
node_modules/react-booking-calendar/lib/index.js:13:10505
- index.js:13 Object.<anonymous>
node_modules/react-booking-calendar/lib/index.js:13:11026
- index.js:1 t
node_modules/react-booking-calendar/lib/index.js:1:328
- index.js:12 Object.<anonymous>
node_modules/react-booking-calendar/lib/index.js:12:5444
- index.js:1 t
node_modules/react-booking-calendar/lib/index.js:1:328
- index.js:1 Object.<anonymous>
node_modules/react-booking-calendar/lib/index.js:1:519
- index.js:1 t
node_modules/react-booking-calendar/lib/index.js:1:328
- index.js:1
node_modules/react-booking-calendar/lib/index.js:1:401
- index.js:1
node_modules/react-booking-calendar/lib/index.js:1:437
- index.js:1
node_modules/react-booking-calendar/lib/index.js:1:65
- index.js:1 Object../node_modules/react-booking-calendar/lib/index.js
node_modules/react-booking-calendar/lib/index.js:1:211
它在错误之前打印整个缩小版本的 react-booking-calendar index.js。在那个 blob 中搜索,我可以看到“self”出现一次:
f=h(function(){return/msie [6-9]\b/.test(self.navigator.userAgent.toLowerCase())})
我将不胜感激有关此问题的任何建议。谢谢。
Gatsby 版本:Gatsby CLI 版本:2.12.8 节点版本:v13.13.0 React 预订日历:^1.0.3
解决方案
正如@Hades 指出的那样,Gatsby 的行为可能会因资产编译和 JavaScript 捆绑而develop
有所不同。您可以在Gatsby 的有关调试的文档build
中查看更多信息。
要绕过您的错误并进行编译,您有几个选项:
等待直到
window
在您使用的组件/函数中定义f=h(function(){return/msie [6-9]\b/.test(self.navigator.userAgent.toLowerCase())})
。使用这样的东西:if (typeof window !== 'undefined') { f=h(function(){return/msie [6-9]\b/.test(self.navigator.userAgent.toLowerCase())}) }
更改 webpack 的配置以使用
null
第三方库的加载器,在您的gatsby-node.js
:exports.onCreateWebpackConfig = ({ stage, loaders, actions }) => { if (stage === "build-html") { actions.setWebpackConfig({ module: { rules: [ { test: /react-booking-calendar/, use: loaders.null(), }, ], }, }) } }
将您的代码放在
componentDidMount
生命周期或useEffect
钩子中,以确保window
在执行代码时已经定义了它。
推荐阅读
- .net - 更新 .NET Core 托管捆绑包需要重新启动 IIS?
- c# - 从 KendoUI 编辑器自定义导出到 word 保存到 DB-屏幕截图没有导出到 word
- kotlin - 我的测试项目中的一些错误与gson的retrofit2
- autohotkey - 按钮自动激活
- angular - Formbuilder反应表单的自定义名称 - Angular 7
- ag-grid - 事件侦听器在网格之外工作,但在 ag-grid 中的单元格内不起作用
- google-sheets - 谷歌表格公式计算一列中与另一张表中的多列匹配的值的总和
- python-3.x - 在预测期间使用来自 tensorflow hub 的 Elmo 作为自定义 tf.keras 层的问题
- perl - 如何在perl中的列表中间添加一个项目
- angular - 打字稿:花括号作为函数参数