【问题标题】:gatsby require.resolve cant find module in createPagegatsby require.resolve 在 createPage 中找不到模块
【发布时间】:2021-03-17 02:18:02
【问题描述】:

我正在学习https://www.gatsbyjs.com/docs/using-gatsby-without-graphql/ 此处的教程,并填写了我的 gatsby-node.js。

我当前的目录结构是:

public
src
-pages
-templates
--program_group.js
static
gatsby-config.js
gatsby-node.js

在我的 gatsby-node.js 文件中

programGroups.forEach(program => {
      createPage({
        path: `/programs/${program}/`,
        component: require.resolve("src/templates/program_group.js"),
        context: { program },
      })
    })

当我运行 gatsby develop 时,它说“找不到模块 'src/templates/program_group.js'”

我尝试将路径更改为 ./src/..... 和 ../src/ 以及每个组合。文档说它应该是相对于配置文件的,上面的路径是......

想法?

【问题讨论】:

    标签: javascript node.js reactjs graphql gatsby


    【解决方案1】:

    尝试使用path.resolve 使用相对路径。这应该有效:

    const path = require(`path`); // import it outside the function
    
    
        programGroups.forEach(program => {
              createPage({
                path: `/programs/${program}/`,
                component: path.resolve("./src/templates/programGroup.js"),
                context: { program },
              })
            })
    

    由于命名问题,您不能在模板下划线,这是要避免的另一点。

    【讨论】:

      【解决方案2】:

      所以,奇怪的是,模板文件名中不能有下划线。这应该在 gatsby 编译器中修复。 解决方案:不要在模板名称中使用 _

      【讨论】:

        猜你喜欢
        • 2020-01-17
        • 2022-01-11
        • 2021-12-31
        • 2018-02-01
        • 1970-01-01
        • 2020-11-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多