Nuxt 项目安装时报错(TypeError: fetch failed)的解决

在安装 Nuxt 项目时报错:ERROR Error: Failed to download template from registry,解决方式是在本地的 hosts 文件中加一行配置:185.199.108.133 raw.githubusercontent.com

主页 > 博客 > Nuxt 项目安装时报错(TypeError: fetch failed)的解决

在安装 Nuxt 项目时报错:ERROR Error: Failed to download template from registry: Failed to download https://raw.githubusercontent.com/nuxt/starter/templates/templates/v3.json: TypeError: fetch failed.

Nuxt 项目安装时报错(TypeError: fetch failed)的解决

报错原因

可能是因为国内的网络政策,对 raw.githubusercontent.com 进行了 DNS 污染,这会导致你的请求在到达 DNS 服务器后解析到错误的服务器 IP 地址,从而无法正确连接到 githubusercontent 的服务,所以当 ping 这个地址的时候也是 ping 不通的(ping raw.githubusercontent.com)。

Nuxt 项目安装时报错(TypeError: fetch failed)的解决

解决办法:在本地配 Host

在本地的 hosts 文件中新加一行配置,即可解决报错问题:

185.199.108.133 raw.githubusercontent.com

上面的配置可以模拟 DNS,这里的 IP 地址你可以在https://sites.ipaddress.com/raw.githubusercontent.com/查到,不过通常都是185.199.108.133,关于配置方法,见下文。

Nuxt 项目安装时报错(TypeError: fetch failed)的解决

Windows

下面是在 Windows 中编辑 Hosts 文件的步骤:

1.打开 Notepad 作为管理员

在开始菜单中搜索 "Notepad",右键点击 "Notepad" 并选择 "以管理员身份运行"。

Nuxt 项目安装时报错(TypeError: fetch failed)的解决

2.打开 Hosts 文件

在 Notepad 中,点击 "文件" 菜单,然后选择 "打开"。导航到以下路径:C:\Windows\System32\drivers\etc\, 然后打开"hosts"文件。

Nuxt 项目安装时报错(TypeError: fetch failed)的解决

3.编辑 Hosts 文件

在文件的末尾添加新的行,每行的格式是 IP 地址,空格,域名,将下面的配置添加进去即可。

185.199.108.133 raw.githubusercontent.com

这会将 raw.githubusercontent.com 映射到主机(185.199.108.133)。

4.保存文件

在 Notepad(记事本) 中编辑完 Hosts 文件后,保存即可。

5.创建 Nuxt 项目:

Hosts 配置后,我们可以再 ping 一下(ping raw.githubusercontent.com),发现成功了:

Nuxt 项目安装时报错(TypeError: fetch failed)的解决

Nuxt 项目也创建成功( npx nuxi@latest init next-demo):

Nuxt 项目安装时报错(TypeError: fetch failed)的解决

macOS

macOS 的 Hosts 文件地址是/etc/hosts,也是在文件后面新增一行:

185.199.108.133 raw.githubusercontent.com

注意事项

编辑 Hosts 文件需要管理员权限,如果你没有管理员权限,你可能无法保存对 Hosts 文件的更改。

Nuxt 项目安装时报错(TypeError: fetch failed)的解决
在安装 Nuxt 项目时报错:ERROR Error: Failed to download template from registry,解决方式是在本地的 hosts 文件中加一行配置:185.199.108.133 raw.githubusercontent.com

了解更多: