518 字
3 分钟
科学使用npm、yarn、node和nvm

问题#

npm、yarn和nvm指令有时会遇到些不可抗力问题。

nvm是解决node版本不兼容导致项目无法启动问题的node版本管理工具

方法#

给npm设置代理#

Terminal window
启动
$ npm config set proxy http://server:port
$ npm config set https-proxy http://server:port
取消
npm config delete proxy
npm config delete https-proxy
查看配置项
npm config list

在wsl2中设置代理#

  1. 据网上说,wsl2中子系统是无法通过localhost直接访问的。因此需要知道wsl2的ip地址。执行以下命令即可。

    Terminal window
    cat /etc/resolv.conf
    # This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
    # [network]
    # generateResolvConf = false
    nameserver 172.24.224.1
  2. 查看window环境下代理软件的端口,并设置。注意重启终端后理论上会失效,需要重新输入。

    Terminal window
    export https_proxy=http://172.24.224.1:10809 http_proxy=http://172.24.224.1:10809 all_proxy=http://172.24.224.1:10809
  3. 设置git代理

    git config --global proxy.https http://$windows_host:10809
  4. 记得在代理软件中设置允许局域网访问,并且在window 防火墙中放行(直接设置中搜防火墙就可以了)

  5. 访问google验证是否设置成功

    curl -vv www.google.com
    curl -i www.google.com

指定淘宝源安装#

注意

  1. yarn和npm不一样,npm设置registry会覆盖lock文件中的依赖下载来源,yarn@1.22.10则相反,会按照lock文件中的下载地址安装。所以建议改变registry后把本地依赖删了重新装一下。(而且理论上构建的时候就不用配置registry了?)
  2. 阿里源有两个地址registry.nlark.com和registry.npmmirror.com,前者在网上检索可以发现cnpm会用。所以设置阿里源后出现第一个地址的依赖也很正常。
  3. 阿里源曾经用过registry.npm.taobao.org在2021年11月之后宣布要迁到registry.npmmirror.com。

npm

Terminal window
一次性
npm install --registry=https://registry.npmmirror.com
长期
npm config set registry https://registry.npmmirror.com
取消
npm config delete registry
指定单次从官方源安装
npm i @daocloud-proto/ghippo --registry=https://registry.npmjs.org

yarn

Terminal window
长期
yarn config set registry https://registry.npmmirror.com/
取消
yarn config delete registry

使用nrm管理源#

Terminal window
npm install -g nrm
nrm ls
nrm use taobao

nvm指定淘宝源#

Terminal window
nvm node_mirror https://npmmirror.com/mirrors/node/
nvm npm_mirror https://npmmirror.com/mirrors/npm/

nvm设置代理#

Terminal window
nvm proxy localhost:10809

经测试(当前版本1.1.7)只支持HTTP代理

科学使用npm、yarn、node和nvm
https://darknesschaser.github.io/2019/02/05/科学使用npm、yarn、node和nvm/
作者
漆黑菌
发布于
2019-02-05
许可协议
CC BY-NC-SA 4.0