gpt4 book ai didi

javascript - WebpackError : ReferenceError: window is not defined on Gatsby

转载 作者:行者123 更新时间:2023-12-05 00:28:41 27 4
gpt4 key购买 nike

我已经在互联网上进行了大量搜索,但无法解决这个问题。
我正在使用 Gasby 开发一个静态页面,但我遇到了这个错误:

WebpackError: ReferenceError: window is not defined
我的线索是,这与我正在使用的 bootsrap/Modal 模块有关。但是我已经清理了我所有的 index.js 并且当我尝试构建它时仍然得到错误。
//index.js
import React from 'react'

const IndexPage = () => (
<div>
</div>
)
export default IndexPage
有人知道我该如何解决吗?谢谢!
Ps:我已经尝试在componentDidMount上导入引导模块,我也尝试设置gatsby-node.js,我还尝试使用可加载组件导入引导模块。
Edit1:来自 gatsby-config.js 的插件部分
  plugins: [
`gatsby-plugin-react-helmet`,
{
resolve: `gatsby-source-filesystem`,
options: {
name: `images`,
path: `${__dirname}/src/images`,
},
},
`gatsby-transformer-sharp`,
`gatsby-plugin-sharp`,
{
resolve: `gatsby-plugin-manifest`,
options: {
name: `ayo`,
short_name: `ayo`,
start_url: `/`,
background_color: `#fff`,
theme_color: `#20336C`,
display: `minimal-ui`,
icon: `src/images/icon.png`, // This path is relative to the root of the site.
},

},
// this (optional) plugin enables Progressive Web App + Offline functionality
// To learn more, visit: https://gatsby.dev/offline
// `gatsby-plugin-offline`,
],

最佳答案

当使用第三方依赖项(例如您的 Bootstrap 模式)时,您可以访问 window对象消失。在这种情况下,您必须添加 null加载器到你的 webpack 的这个模块的配置。
在您的 gatsby-node.js :

exports.onCreateWebpackConfig = ({ stage, loaders, actions }) => {
if (stage === "build-html") {
actions.setWebpackConfig({
module: {
rules: [
{
test: /bad-module/,
use: loaders.null(),
},
],
},
})
}
}
在上面的代码中,您必须替换 /bad-module/ node_modules 中的依赖文件夹你想避免转译。基本上,您在服务器渲染期间用一个虚拟模块替换有问题的模块,因为它是一个正则表达式,您必须将您的模块名称与文件夹匹配。
您可以在 Gatsby's documentation about debugging HTML builds 中查看更多信息.

关于javascript - WebpackError : ReferenceError: window is not defined on Gatsby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63381461/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com