Got error when check update: Connect timeout for 5000ms, GET https://registry.npmjs.org/egg-init/lat

本文介绍了解决Egg项目创建过程中遇到的问题,通过更换npm源到淘宝源,成功创建了Egg应用。

在egg的时候,根据文档的方式创建项目,但是一直失败:

suchcl@DESKTOP-EH6OH7F:/mnt/d/WebStudy/EggApp$ npm init egg --type=simple
npx: installed 396 in 20.017s
[egg-init] use registry: https://registry.npmjs.org
Got error when check update: Connect timeout for 5000ms, GET https://registry.npmjs.org/egg-init/latest -2 (connected: false, keepalive socket: false, socketHandledRequests: 1, socketHandledResponses: 0)
headers: {}
[egg-init] target dir is /mnt/d/WebStudy/EggApp
[egg-init] fetching npm info of egg-init-config
[egg-init] use fallback from egg-init-config
[egg-init] use boilerplate: simple(egg-boilerplate-simple)
[egg-init] fetching npm info of egg-boilerplate-simple
ResponseTimeoutError: Response timeout for 5000ms, GET https://registry.npmjs.org/egg-boilerplate-simple/latest 200 (connected: true, keepalive socket: false, socketHandledRequests: 1, socketHandledResponses: 1)
headers: {"date":"Tue, 10 Nov 2020 06:05:57 GMT","content-type":"application/json","transfer-encoding":"chunked","connection":"keep-alive","set-cookie":["__cfduid=dd4e69410af7f56375659ca707afa43481604988357; expires=Thu, 10-Dec-20 06:05:57 GMT; path=/; domain=.npmjs.org; HttpOnly; SameSite=Lax"],"cf-ray":"5efd95b058280280-SJC","vary":"Accept-Encoding","cf-cache-status":"DYNAMIC","cf-request-id":"06525be23900000280f70ea000000001","expect-ct":"max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\"","server":"cloudflare"}
    at Timeout._onTimeout (/home/suchcl/.npm/_npx/3154/lib/node_modules/create-egg/node_modules/urllib/lib/urllib.js:960:15)
    at ontimeout (timers.js:436:11)
    at tryOnTimeout (timers.js:300:5)
    at listOnTimeout (timers.js:263:5)
    at Timer.processTimers (timers.js:223:10)

根源是不能正常使用https://registry.npmjs.org这个源,那我们就通过设置指定源的方式创建项目就可以了:

npm init egg -r=https://registry.npm.taobao.org 指定taobao源

suchcl@DESKTOP-EH6OH7F:/mnt/d/WebStudy/EggApp/egg2$ npm init egg -r=https://registry.npm.taobao.org --type=simple
[##................] \ loadDep:socks-proxy-agent: sill resolveWithNewModule yallist@3.1.1 checking installable status
npx: installed 396 in 160.073s
[egg-init] use registry: https://registry.npm.taobao.org
[egg-init] target dir is /mnt/d/WebStudy/EggApp/egg2
[egg-init] fetching npm info of egg-init-config
[egg-init] use boilerplate: simple(egg-boilerplate-simple)
[egg-init] fetching npm info of egg-boilerplate-simple
[egg-init] downloading https://registry.npm.taobao.org/egg-boilerplate-simple/download/egg-boilerplate-simple-3.3.1.tgz
[egg-init] extract to /tmp/egg-init-boilerplate
[egg-init] collecting boilerplate config...
? project name egg2
? project description egg2
? project author yanshijing
? cookie security keys 1604992596502_3035
[egg-init] write to /mnt/d/WebStudy/EggApp/egg2/.autod.conf.js
[egg-init] write to /mnt/d/WebStudy/EggApp/egg2/.eslintignore
[egg-init] write to /mnt/d/WebStudy/EggApp/egg2/.eslintrc
[egg-init] write to /mnt/d/WebStudy/EggApp/egg2/README.md
[egg-init] write to /mnt/d/WebStudy/EggApp/egg2/.gitignore
[egg-init] write to /mnt/d/WebStudy/EggApp/egg2/package.json
[egg-init] write to /mnt/d/WebStudy/EggApp/egg2/app/router.js
[egg-init] write to /mnt/d/WebStudy/EggApp/egg2/config/config.default.js
[egg-init] write to /mnt/d/WebStudy/EggApp/egg2/config/plugin.js
[egg-init] write to /mnt/d/WebStudy/EggApp/egg2/app/controller/home.js
[egg-init] write to /mnt/d/WebStudy/EggApp/egg2/test/app/controller/home.test.js
[egg-init] usage:
      - cd /mnt/d/WebStudy/EggApp/egg2
      - npm install
      - npm start / npm run dev / npm test

创建成功,问题解决。

在尝试从 npm registry 获取特定版本的包时遇到 404 错误,通常表示请求的资源不存在或无法访问。对于 GET 请求 `https://registry.npmjs.org/v20.19.0` 返回 404 的情况,可能是以下原因导致: 1. **请求的路径格式错误**:npm registry 的标准请求路径通常遵循 `https://registry.npmjs.org/<package-name>/-/<package-name>-<version>.tgz` 的格式,而不是直接访问类似 `v20.19.0` 的路径。如果直接访问版本号命名的路径,可能导致无效请求[^1]。 2. **版本不存在**:请求的版本 `v20.19.0` 可能并不存在于 registry 中。某些包可能使用不同的版本命名规则(例如,省略前缀 `v` 或使用语义化版本号如 `20.19.0`)。可以通过访问 `https://registry.npmjs.org/<package-name>` 查看所有可用版本。 3. **网络或代理问题**:如果用户使用了代理或私有 registry(如 Verdaccio、Artifactory 等),可能存在配置错误或缓存问题,导致请求被错误地转发或拦截。 4. **npm registry 临时故障**:尽管较为罕见,但 npm registry 本身可能因临时性问题导致某些请求失败。可以尝试更换网络环境或稍后重试。 5. **权限问题**:如果请求的包为私有包,但未正确配置访问令牌或权限,也可能导致 404 错误(部分情况下,出于安全考虑,私有资源的未授权访问会返回 404 而非 403)。 ### 解决方案 - **验证请求路径格式**:确保请求的 URL 符合 npm registry 的标准格式。例如,获取 `lodash` 的 `4.17.19` 版本应使用 `https://registry.npmjs.org/lodash/-/lodash-4.17.19.tgz`。 - **检查版本号是否存在**:通过访问 `https://registry.npmjs.org/<package-name>` 获取所有可用版本列表,确认目标版本是否存在于 `versions` 字段中。 - **清除缓存与配置检查**:运行 `npm cache clean --force` 清除本地缓存,并检查 `.npmrc` 文件是否配置了错误的 registry 或代理。 - **使用 `npm view` 命令调试**:执行 `npm view <package-name>@<version>` 可以查看指定版本的元数据,确认其是否存在。 - **切换 registry**:使用 `npm config set registry https://registry.npmjs.org` 确保当前 registry 指向官方源。如果使用私有源,需确保其配置正确且目标版本已同步。 ### 示例:检查包版本是否存在 ```bash npm view express@4.18.2 ``` 如果版本存在,该命令将输出包的元数据;如果版本不存在,则会提示 `dist-tags for requested package are not available`。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值