记 npm,git,bower 安装文件时的坑

735 查看

首先交代故事的起因。vue2.0发布了,随之vue-cli脚手架工具也迎来了版本更新-2.4,
本以为新特性会带来一些惊喜。带着愉悦的心情开始一个新项目vue init webpack project-name
弹出提示:

This will install Vue 2.x version of template.
For Vue 1.x use: Vue init webpack#1.0 vueproject

很人性化嘛,还提示版本初始化区别的命令。于是重新执行vue init webpack#1.0 project-name,始料未及的是:

vue-cli · connect ETIMEDOUT 192.30.252.137:443

类似这种错误,大致是因为网络代理的原因。接触git bash这款命令行工具很久了,虽然很强大,但也有一些命令是需要切换回cmd进行处理的。

这里解释下为什么不用2.4版本初始化项目。
其一,从vue1.0版本过渡到2.0需要时间,项目已经立项。
其二,vue2.0版本刚刚推出,自己觉得还得等待一段时间才适合投入项目进行开发。

这里来整理下目前存在的问题:

  1. git base自身不够完善(win下),有时需要切回cmd模式;

  2. 执行npm,git,bower等操作时,会时不时因网络代理而失败;

依次解决以上问题:

重装 git bash

  1. 卸载git bash

  2. 重新安装git bash

  3. 安装时需要的设置:

解决代理问题

有时候我们在某些环境下(比如墙内或公司内网)可能不能正常使用 git/npm/bower/gem 等各种工具,那就使用代理吧。这里假定服务器代理地址为127.0.0.1,端口为1080

  1. 为 npm 设置代理

    npm config set proxy http://127.0.0.1:1080
    npm config set https-proxy http://127.0.0.1:1080

    注意不要遗漏 http:// ,否则可能安装某些package时可能会报:

    ERR! Error: Invalid protocol

    如果代理需要认证的话可以这样来设置:

    npm config set proxy http://username:password@server:port
    npm config set https-proxy http://username:pawword@server:port
  2. 为 git 设置代理

    • 第一种设置代理的方式

      git config --global http.proxy http://127.0.0.1:1080
      git config --global https.proxy http://127.0.0.1:1080

      由于代理上网,git 协议不能用,所以需要设置来用 https 协议替代:

      git config --global url."https://".insteadOf git://

      如果执行:

      git clone https://....

      报以下错误:

      error: server certificate verification failed.
      CAfile: /etc/ssl/certs/ca-certificates.crt
      CRLfile: none

      说明证书校验有问题,可以设置:

      git config --global http.sslverify false
    • 第二种方法是直接修改git的配置文件.gitconfig

      [http]
      proxy = http://10.24.48.191:808
      sslverify = false
      [https]
      proxy = http://10.24.48.191:808
      [url "https://"]
      insteadOf = git://
  3. 为 bower 设置代理

    bower 的安装和使用依赖于 npm 和 git,在完成前两者的代理设置后,有两种方法为 bower 设置代理。

    • 方法一直接修改 bower 的配置文件.bowerrc,如果没有可以添加:

      "proxy": "http://10.24.48.6:808",
      "https-proxy": "http://10.24.48.6:808"
    • 在系统变量里添加 HTTP_PROXY 和 HTTPS_PROXY。

代理设置参考链接;
git重装参考链接;