Gatsby踩坑记录

gatsby-node.js 中出现 (void 0) is not a function

在将gatsby-node.js转换为gatsby-node.ts之后出现错误:

ERROR #11321  PLUGIN

"gatsby-node.js" threw an error while running the createPages lifecycle:

(void 0) is not a function

File: .cache/compiled/gatsby-node.js:1:1887

  TypeError: (void 0) is not a function

如果只看报错内容,这是一个比较隐晦的错误,直接Google没法得到什么有用的信息,Traceback也只有混淆后的代码,给定位错误错误带来困难。不过官方文档中,TypeScript and Gatsby的Current limitations一节中是有提到这个问题的。

require.resolve
You can’t use require.resolve in your files. You’ll need to replace these instances with a path.resolve call.

需要使用path.resolve替换require.resolve,就像这样:

+ import path from "path"

+ const template = path.resolve(`src/templates/template.tsx`)
- const template = require.resolve(`./src/templates/template.tsx`)

本文链接:

https://www.direcore.xyz/archives/34/
1 + 9 =
快来做第一个评论的人吧~