iblogc

解决 NPM Nuxt 3 项目初始化报错 ERROR Error: Failed to download template from registry

2024-06-08 · 3 min read

npx nuxi@latest init demo 初始化 nuxt 3 项目时报以下错误

[01:29:58]  ERROR  Error: Failed to download template from registry: Failed to download https://raw.githubusercontent.com/nuxt/starter/templates/templates/v3.json: TypeError: fetch failed

确认问题

执行下面命令确认问题:

node -e "require('https').get('https://raw.githubusercontent.com/nuxt/starter/templates/templates/v3.json')"

问题一及解决方案

如果报以下错,则是网络问题,可以使用改host、改DNS、开代理其中一种解决问题

node:events:491
      throw er; // Unhandled 'error' event
      ^

Error: connect ECONNREFUSED 0.0.0.0:443
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1247:16)
Emitted 'error' event on ClientRequest instance at:
    at TLSSocket.socketErrorListener (node:_http_client:481:9)
    at TLSSocket.emit (node:events:513:28)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  errno: -61,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '0.0.0.0',
  port: 443
}

解决方案一:改host
打开 https://sites.ipaddress.com/raw.githubusercontent.com/ 获取 raw.githubusercontent.com 的 ip,修改host

185.199.108.133 raw.githuusercontent.com
185.199.109.133 raw.githuusercontent.com
185.199.110.133 raw.githuusercontent.com
185.199.111.133 raw.githuusercontent.com

解决方案二:改DNS
切换为 Google DNS 8.8.8.8 8.8.4.4

解决方案三:开全局代理/shell临时代理(自行google)

问题二及解决方案

如果报以下错,则是证书问题,可以使用改host、改DNS、开代理其中一种解决问题

node:events:497
      throw er; // Unhandled 'error' event
      ^

Error: unable to verify the first certificate
    at TLSSocket.onConnectSecure (node:_tls_wrap:1674:34)
    at TLSSocket.emit (node:events:519:28)
    at TLSSocket._finishInit (node:_tls_wrap:1085:8)
    at ssl.onhandshakedone (node:_tls_wrap:871:12)
Emitted 'error' event on ClientRequest instance at:
    at TLSSocket.socketErrorListener (node:_http_client:500:9)
    at TLSSocket.emit (node:events:519:28)
    at emitErrorNT (node:internal/streams/destroy:169:8)
    at emitErrorCloseNT (node:internal/streams/destroy:128:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  code: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE'
}```

解决方案一(*我用此该当未解决问题*):
```shell
npm config set strict-ssl false

解决方案二(我的解决方案):
设置环境/临时shell变量

Windows CMD

set NODE_TLS_REJECT_UNAUTHORIZED=0

Windows PowerShell

$env:NODE_TLS_REJECT_UNAUTHORIZED = "0"

其它系统自行google

这样之后再执行 npx nuxi@latest init demo 命令就成功了,只是会出现NODE_TLS_REJECT_UNAUTHORIZED 设置为 0 不安全的警告,不用管它

参考资料: