构建静态HTML页面出错:错误:ENOENT:没有这个文件或目录。

6

我有一个 Gatsby 项目,它能够正常工作,并且也有 404 页面。但是在执行 gatsby build 或者成功完成构建进程后打开浏览器时,会出现错误。

Building static HTML for pageserror: Error: ENOENT: no such file or directory

如果我在gatsby build时没有遇到错误,那么当我运行 gatsby serve 并打开浏览器时可能会出现错误:

Error: ENOENT: no such file or directory, stat '/project/public/404.html'

1
尝试添加404页面 - user13933230
1
当你运行 gatsby clean 然后再运行 gatsby buildgatsby serve 时会发生什么? - ShinaBR2
1
@ShinaBR2已经尝试过rm -rf node_module .cache public,但是没有帮助我解决问题。虽然之前它是可以工作的! - Ericgit
3个回答

5
// clean gatsby cache
gatsby clean
// delete node modules, cache and public folder.
rm -rf node_module .cache public
// reinstall node modules
npm install
// run gatsby in develop mode
gatsby develop
// build the gatsby project
npm run build
// serve the building project.
gatsby serve

即使只运行 gatsby clean 命令也可以解决这种问题。此外,我建议您将开发命令设置为 gatsby develop -H 0.0.0.0,这样可以在连接到同一网络的情况下进行实时移动测试。 - doublejosh

4

解决方法1

如果没有的话,请在 pages 文件夹中添加404.js404.tsx文件。

  1. 尝试卸载build.cachenode_modules
  2. yarn add pkg.json npm install *
  3. 将所有需要的属性传递给使用了该布局(comp)的所有页面中的布局。
  4. 在运行gatsby build之前,请运行gatsby develop

解决方法2

可能你正在组件中使用不支持服务器端渲染的React.lazysuspense。你需要通过任何可能的方式移除它。然后尝试这个方案应该可以解决问题!

想要在Gatsby中使用React.lazy


1
如果上述答案对您没有用(对我也没用),Gatsby 在这里保留了一个 config.json 的副本 ~/.config/gatsby/config.json
删除该文件后,它对我有效。
来自 Gatsby 存储库的 相关 Github 问题

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接