Solution to Nuxt3 Project Initialization Failure

In China mainland, the project initialization command of Nuxt will encounter the following error.

Error: Failed to download template from registry: Failed to download https://raw.githubusercontent.com/nuxt/starter/templates/templates/v3.json: TypeError: fetch failed

The reason seems to be that during project initialization, it will download the template information from GitHub, and the download address of raw.githubusercontent.com cannot be directly accessed due to DNS pollution, so the access fails, and the project initialization naturally fails.

In this case, one naturally wonders if it's because the proxy is not used. Trying to use a global proxy doesn't work. I thought it was because the terminal did not open the proxy, and added a proxy environment variable, but it still didn't work. I searched online and found the corresponding issue directly in the official repo. Many people have encountered this problem. The majority of viable solutions mentioned in it are achieved by modifying /etc/hosts, but I did not want to change system file settings, so I did not try this method.

Why can't the proxy be set through environment variables? Looking at the content of the issue, it is because Nuxt3 uses giget to download templates from the GitHub repository, and this giget repository uses the native node-fetch to implement the download function. This native fetch does not support the use of proxies, so even if the environment variable is set, the proxy cannot be used for downloading, causing the project to fail to initialize.

Fortunately, I saw @hzgotb found a solution by looking through the source code: The download link of this file will be fetched from the environment variable NUXI_INIT_REGISTRY first, so in order to solve the download problem, this guy directly cloned a repo to gitee, which ensures access.

So you only need to specify the environment variable to initialize the project, and the problem can be solved.

1
2
3
4
5
6
# macos & Linux
export NUXI_INIT_REGISTRY="https://gitee.com/hzgotb/nuxt-starter/raw/templates/templates" && npx nuxi@latest init nuxt-blog

# powershell
$env:NUXI_INIT_REGISTRY="https://gitee.com/hzgotb/nuxt-starter/raw/templates/templates"
npx nuxi@latest init nuxt-blog