不管是使用egg-init 还是按照egg官网上提供的方法,npm init egg --type=simple的方式构建项目,都会因为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)
总之,就是因为无法访问npm,https://register.npmjs.org不能正常访问导致的。
后来使用egg-init构建项目,也是出现同样的问题,要解决问题,就要解决访问npm源的问题。
通过egg-init的帮助文档:
D:\WebStudy\EggApp\egg3>egg-init -h
init egg project from boilerplate.
Usage: egg-init.js [dir] --type=simple
Options:
--type boilerplate type [string]
--dir target directory [string]
--force, -f force to override directory [boolean]
--template local path to boilerplate [string]
--package boilerplate package name [string]
--registry, -r npm registry, support china/npm/custom, default to auto detect
[string]
--silent don't ask, just use default value [boolean]
--version Show version number [boolean]
-h, --help Show help [boolean]
看到了--registry, -r这个选项,可以设置npm的源,然后在构建项目的时候设置这么个参数,把值设置为淘宝的npm镜像:https://registry.npm.taobao.org
# npm-init初始化项目的时候,需要项目名称,而直接使用npm init egg初始化项目的时候,直接取当前代码所在的目录名为项目名称
egg-init eggProjectName -r=https://registry.npm.taobao.com
#直接npm init也是可以的
#使用npm init初始化项目的时候,是不用跟随项目名称的,默认取当前的目录名为项目名称
npm init egg -r=https://registry.npm.taobao.org
分别使用两种方式创建初始化项目:
npm-init方式创建、初始化项目:
D:\WebStudy\EggApp\egg4>egg-init egg5 -r=https://registry.npm.taobao.org
[egg-init] use registry: https://registry.npm.taobao.org
[egg-init] target dir is D:\WebStudy\EggApp\egg4\egg5
[egg-init] fetching npm info of egg-init-config
? Please select a boilerplate type simple - Simple egg app boilerplate
[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 C:\Users\ya596\AppData\Local\Temp\egg-init-boilerplate
[egg-init] collecting boilerplate config...
? project name egg5
? project description
? project author
? cookie security keys 1604994587824_7627
[egg-init] write to D:\WebStudy\EggApp\egg4\egg5\.autod.conf.js
[egg-init] write to D:\WebStudy\EggApp\egg4\egg5\.eslintignore
[egg-init] write to D:\WebStudy\EggApp\egg4\egg5\.eslintrc
[egg-init] write to D:\WebStudy\EggApp\egg4\egg5\README.md
[egg-init] write to D:\WebStudy\EggApp\egg4\egg5\.gitignore
[egg-init] write to D:\WebStudy\EggApp\egg4\egg5\package.json
[egg-init] write to D:\WebStudy\EggApp\egg4\egg5\app\router.js
[egg-init] write to D:\WebStudy\EggApp\egg4\egg5\config\config.default.js
[egg-init] write to D:\WebStudy\EggApp\egg4\egg5\config\plugin.js
[egg-init] write to D:\WebStudy\EggApp\egg4\egg5\app\controller\home.js
[egg-init] write to D:\WebStudy\EggApp\egg4\egg5\test\app\controller\home.test.js
[egg-init] usage:
- cd D:\WebStudy\EggApp\egg4\egg5
- npm install
- npm start / npm run dev / npm test
npm init egg方式创建、初始化项目:
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
可见,添加上-r参数,重新设置npm源后就可以了。