WebPackError:TypeError:无法读取未定义的属性(读取'组件----- src-templates-category-js')
我正在使用gatsby和我的gatbsy.node-js文件,我正在创建动态页面。
当我运行Gatsby开发时,这很好,但是当我进行盖茨比构建或在Netlify上时,我会在下面收到Errror消息,并且我认为它必须对我的模板的路径做一些事情,这是因为我的计算机是本地的盖茨比(Gatsby Build)想要另一种类型的路径?
我还可以在webpackerror中看到它从(组件--- src-templates-category-js)读取而不是没有连字符的.js,这是正确的吗?
如果我用 require.resolve and path。从“路径”溶解,我会得到相同的错误)
const { slash } = require('gatsby-core-utils')
const categoryTemplate = require.resolve(`./src/templates/category.js`)
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const queryCategories = await graphql(`
query AllCategories {
allWpProductCategory {
nodes {
id
slug
}
}
}
`)
queryCategories.data.allWpProductCategory.nodes.forEach(node => {
createPage({
path: `/product-category/${node.slug}`,
component: slash( categoryTemplate ),
context: {
category: node.id,
},
})
})
ERROR
Page data from page-data.json for the failed page "/product-category/chocolate": {
"componentChunkName": "component---src-templates-category-js",
"path": "/product-category/chocolate",
"result": {
"pageContext": {
"category": "dGVybTo1OQ=="
}
},
"staticQueryHashes": [
"3971950002"
]
}
failed Building static HTML for pages - 4.746s
ERROR #95313
Building static HTML failed for path "/product-category/chocolate"
WebpackError: TypeError: Cannot read properties of undefined (reading 'component---src-templates-category-js')
Im using gatsby and in my gatbsy.node-js file I am creating dynamic pages.
This is working fine when I run gatsby develop but when I do gatsby build or on netlify, I get the errror message below and im thinking it has to do something with the path to my template, is it because the path is local to my computer and gatsby build wants another type of path?
I can also see in the WebpackError that its reading from (component---src-templates-category-js) instead of .js without a hyphen, is that correct?
I get the same exact error if I import my template with require.resolve and path.resolve from "path", aswell with and without slash (from gatsby-core-utils)
const { slash } = require('gatsby-core-utils')
const categoryTemplate = require.resolve(`./src/templates/category.js`)
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const queryCategories = await graphql(`
query AllCategories {
allWpProductCategory {
nodes {
id
slug
}
}
}
`)
queryCategories.data.allWpProductCategory.nodes.forEach(node => {
createPage({
path: `/product-category/${node.slug}`,
component: slash( categoryTemplate ),
context: {
category: node.id,
},
})
})
ERROR
Page data from page-data.json for the failed page "/product-category/chocolate": {
"componentChunkName": "component---src-templates-category-js",
"path": "/product-category/chocolate",
"result": {
"pageContext": {
"category": "dGVybTo1OQ=="
}
},
"staticQueryHashes": [
"3971950002"
]
}
failed Building static HTML for pages - 4.746s
ERROR #95313
Building static HTML failed for path "/product-category/chocolate"
WebpackError: TypeError: Cannot read properties of undefined (reading 'component---src-templates-category-js')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
,只要Netlify抛出
gatsby build
,因此,如果构建在本地失败,则可能会在Netlify中失败。关于问题,我不知道您为什么使用
slash
(const {slash} = require('gatsby-core-utils')
)。您是否尝试过一些简单的东西:请注意删除
slash
以及path
在path.resolve中的使用(`./src/templates/category.js `)
。This makes sense as long as Netlify throws a
gatsby build
, so if a build is failing locally it will probably fail in Netlify.Regarding the issue, I don't know why you are using
slash
(const { slash } = require('gatsby-core-utils')
). Have you tried something simpler like:Note the removal of the
slash
and the use ofpath
inpath.resolve(`./src/templates/category.js`)
.