Skip to content

nuxt 初始化项目失败

背景

使用pnpm dlx nuxi init初始化项目报模板拉取失败

bash
Error: Failed to download template from registry: fetch failed

分析

定位到 nuxi init:

bash
Command failed with exit code 1: nuxi init

去看看 nuxi 是在哪里拉取的模板,这个是nuxi cli

进到init 命令位置

nuxi init定义了默认的模板仓库

ts
// ...
const DEFAULT_REGISTRY =
  "https://raw.githubusercontent.com/nuxt/starter/templates/templates";
// ...

由于 DNS 污染,没有代理的情况下一般访问不了raw.githubusercontent.com,所以导致项目初始化失败。

解决办法

配置process.env.NUXI_INIT_REGISTRY, 可以自己搭建 gitea 或者别的代码管理仓库

TIP

在下载模板的时候会读取process.env.NUXI_INIT_REGISTRY用来读取自定 义模板仓库

ts
// ...
await downloadTemplate(template, {
  dir: args._[0] as string,
  force: args.force,
  offline: args.offline,
  preferOffline: args["prefer-offline"],
  registry: process.env.NUXI_INIT_REGISTRY | DEFAULT_REGISTRY,
});
// ...

设置环境变量

powershell
$Env:NUXI_INIT_REGISTRY = "http://120.78.174.154:3000/baizhi958216/starter/raw/branch/templates/templates"

初始化项目

powershell
pnpm dlx nuxi init

success