You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gatsby运行报错TypeError: createNodeId is not a function该如何解决?

错误原因

Gatsby 的 exports.sourceNodes API 仅接收一个对象类型的入参,所有可用的工具方法和配置项都包含在这个对象中。你当前的写法把 createNodeId、createContentDigest 拆成了独立的第二、第三个入参,实际上这两个方法并没有被正确传入,因此调用时会抛出「不是函数」的类型错误。

修复方案

修改 exports.sourceNodes 的入参写法,从第一个对象中解构出需要的方法即可,修正后的代码如下:

// 入参改为从第一个对象中解构所需字段
exports.sourceNodes = async ({ actions, createNodeId, createContentDigest }) => {
  const res = await axios.get("https://jsonplaceholder.typicode.com/posts")
  const posts = res.data

  posts.forEach(post => {
    const node = {
      title: post.title,
      body: post.body,
      id: createNodeId(`Post-${post?.id}`),
      parent: null,
      children: [],
      internal: {
        type: "Post",
        contentDigest: createContentDigest(post),
        content: JSON.stringify(post)
      }
    }

    actions.createNode(node)
  })
}

内容的提问来源于stack exchange,提问作者JorgeL

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.13 22:21:02