【问题标题】:Can’t read siteMetadata object from gatsby-config.js file with Gatsby and Typescript无法使用 Gatsby 和 Typescript 从 gatsby-config.js 文件中读取 siteMetadata 对象
【发布时间】:2019-11-11 14:08:22
【问题描述】:

我正在尝试读取我在 gatsby-config.js 文件中定义的 siteMetadata 对象的内容,但每次尝试都会收到此错误

ERROR in ./src/pages/index.tsx
Module build failed (from ./node_modules/gatsby/dist/utils/babel-loader.js):
TypeError: Cannot read property 'type' of null
    at VariableDeclarator (/mnt/d/Repositories/FnStacks/HandyTrade/handytrade-store-web/node_modules/babel-plugin-remove-graphql-queries/index.js:277:81)
    at NodePath._call
(/mnt/d/Repositories/FnStacks/HandyTrade/handytrade-store-web/node_modules/@babel/traverse/lib/path/context.js:53:20)
    at NodePath.call
(/mnt/d/Repositories/FnStacks/HandyTrade/handytrade-store-web/node_modules/@babel/traverse/lib/path/context.js:40:17)
    at NodePath.visit
(/mnt/d/Repositories/FnStacks/HandyTrade/handytrade-store-web/node_modules/@babel/traverse/lib/path/context.js:88:12)
    at TraversalContext.visitQueue
(/mnt/d/Repositories/FnStacks/HandyTrade/handytrade-store-web/node_modules/@babel/traverse/lib/context.js:118:16)
    at TraversalContext.visitMultiple
(/mnt/d/Repositories/FnStacks/HandyTrade/handytrade-store-web/node_modules/@babel/traverse/lib/context.js:85:17)
    at TraversalContext.visit
(/mnt/d/Repositories/FnStacks/HandyTrade/handytrade-store-web/node_modules/@babel/traverse/lib/context.js:144:19)
    at Function.traverse.node
(/mnt/d/Repositories/FnStacks/HandyTrade/handytrade-store-web/node_modules/@babel/traverse/lib/index.js:94:17)
    at NodePath.visit
(/mnt/d/Repositories/FnStacks/HandyTrade/handytrade-store-web/node_modules/@babel/traverse/lib/path/context.js:95:18)
    at TraversalContext.visitQueue
ℹ 「wdm」: Failed to compile.

我正在使用带有打字稿的 Gatsby。我在gatsby-config.js 上安装了插件gatsby-plugin-ts-loader 和所需的设置。我正在尝试在我的项目的起始页中读取 siteMetadata 对象。我尝试使用来自gatsby 库的graphqluseStaticQuery,但它失败了。这是我如何进行的

  const data = useStaticQuery(graphql`
    query IndexQuery {
      site {
        siteMetadata {
          title
        }
      }
    }
  `);

这是我在 gatsby-config.js 中定义 SiteMetada 对象的方式


  siteMetadata: {
    title: 'Saturn web',
    author: 'FunctionalStack',
    description: 'Gatsby with typscript',
    siteUrl: 'http://localhost :8000/'
  }

如果我删除所有读取站点元数据的查询,应用程序将正常运行。我在适用于 Linux 的 Windows 子系统上使用 gatsby - Ubuntu 18.04.1

【问题讨论】:

  • 您的错误不可重现,我的 repo 中的 typescript 设置没有问题。但是有很多变量,如果您可以链接到您的存储库或提供一个显示错误的最小示例,那将会很有帮助!
  • 嗨 Derek,这里是重现错误 https://github.com/dyesseyumba/gatsby-typescript-starter 的 repo
  • 感谢您添加回购!在 tsconfig 中,你会尝试将 jsxreact 更改为 preserve 吗? gatsby 使用 babel 静态移除 graphql 查询,所以如果 ts 移除 jsx,它将无法找到查询。让我知道这是否有效
  • 我在tsconfig.json 中将react 更改为preserve,但仍然会产生同样的错误。
  • 我克隆了你的仓库并进一步检查了——请看下面的答案

标签: node.js reactjs typescript babeljs gatsby


【解决方案1】:

当我在你的根目录中运行 tsc -p . 并检查输出时,我发现 tsc 编译了这个:

  const data = useStaticQuery(graphql`
    query IndexQuery {
      site {
        siteMetadata {
          title
        }
      }
    }
  `);

进入这个:

var data = useStaticQuery(graphql(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n    query IndexQuery {\n      site {\n        siteMetadata {\n          title\n        }\n      }\n    }\n  "], ["\n    query IndexQuery {\n      site {\n        siteMetadata {\n          title\n        }\n      }\n    }\n  "]))));

Gatsby 期望 graphql 查询在模板文字中,但 tsc 将它们编译为常规函数。

要解决此问题,您需要将tsconfig 更改为至少输出es6

// tsconfig.json
{
  compileOptions: {
    "module": "esnext",
    "target": "esnext", <-- or es6 & above
  }
}

顺便说一句,我最近为 gatsby 发布了一个 ts 插件,旨在使在 typescript 中使用 gatsby 尽可能流畅。它所做的不同事情之一是为 graphql 查询自动生成类型。如果您试一试,如果有任何问题,我将不胜感激:

【讨论】:

  • 使用你的插件,你的回答结束了我两个小时沮丧地扯掉头发的过程......谢谢!
  • @HappyKoala 很高兴听到它有帮助,如果您遇到任何问题,请随时提交!
猜你喜欢
  • 2020-10-02
  • 2023-03-09
  • 1970-01-01
  • 2019-10-03
  • 1970-01-01
  • 1970-01-01
  • 2022-10-22
  • 2021-11-23
  • 2020-11-09
相关资源
最近更新 更多