WebPackError:TypeError:无法读取未定义的属性(读取'组件----- src-templates-category-js')

发布于 2025-02-09 04:40:02 字数 1882 浏览 2 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万劫不复 2025-02-16 04:40:02

当我运行盖茨比开发时,这很好,但是当我做盖茨比
构建或在Netlify上,我在下面收到错误消息,然后我认为
必须通过通往我的模板的路径做点事

,只要Netlify抛出gatsby build,因此,如果构建在本地失败,则可能会在Netlify中失败。

关于问题,我不知道您为什么使用slashconst {slash} = require('gatsby-core-utils'))。您是否尝试过一些简单的东西:

const path = require("path")

exports.createPages = async ({ graphql, actions }) => {
  const { createPage } = actions;
  const categoryTemplate = path.resolve(`./src/templates/category.js`);

  const queryCategories = await graphql(`
    query AllCategories {
      allWpProductCategory {
        nodes {
          id
          slug
        }
      }
    }
  `);

  queryCategories.data.allWpProductCategory.nodes.forEach((node) => {
    createPage({
      path: `/product-category/${node.slug}`,
      component: categoryTemplate,
      context: {
        category: node.id,
      },
    });
  });
};

请注意删除slash以及pathpath.resolve中的使用(`./src/templates/category.js `)

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

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:

const path = require("path")

exports.createPages = async ({ graphql, actions }) => {
  const { createPage } = actions;
  const categoryTemplate = path.resolve(`./src/templates/category.js`);

  const queryCategories = await graphql(`
    query AllCategories {
      allWpProductCategory {
        nodes {
          id
          slug
        }
      }
    }
  `);

  queryCategories.data.allWpProductCategory.nodes.forEach((node) => {
    createPage({
      path: `/product-category/${node.slug}`,
      component: categoryTemplate,
      context: {
        category: node.id,
      },
    });
  });
};

Note the removal of the slash and the use of path in path.resolve(`./src/templates/category.js`).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文