【发布时间】: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 库的graphql 和useStaticQuery,但它失败了。这是我如何进行的
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 中,你会尝试将
jsx从react更改为preserve吗? gatsby 使用 babel 静态移除 graphql 查询,所以如果 ts 移除 jsx,它将无法找到查询。让我知道这是否有效 -
我在
tsconfig.json中将react更改为preserve,但仍然会产生同样的错误。 -
我克隆了你的仓库并进一步检查了——请看下面的答案
标签: node.js reactjs typescript babeljs gatsby