环境搭建

  • https://nodejs.org/en/download/ 取得下载链接,wget 下载,tar Jxf xxx.tar.xz
  • 手动建目录 /opt/node 作为根目录
  • 环境变量 ```bash

    vi ~/.bashrc 或者 vi ~/.bash_profile 追加:

    export PATH=${PATH}:/opt/node/bin export PATH=${PATH}:/opt/node/npm_prefix/bin

执行命令

source ~/.bashrc source ~/.bash_profile

  1. <a name="Frjsc"></a>
  2. ## 重要配置
  3. ```bash
  4. npm config set prefix "/opt/node/npm_prefix" // npm 全局安装路径的前缀
  5. npm config set cache "/opt/node/npm_cache" // npm 包缓存位置
  6. npm config set registry "http://registry.npm.taobao.org" // npm 源配置地址
  • 执行命令 npm config get userconfig,可以发现 npm 所有配置保存的文件是:/root/.npmrc
  • 执行命令 npm config list -l 可以获取 npm 当前所有配置

NVM 管理多个版本

参考:https://github.com/nvm-sh/nvm#install—update-script
设个代理:export https_proxy=”132.122.237.162:52080”
安装:curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

  1. nvm ls-remote
  2. nvm ls
  3. export NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node/
  4. nvm install 14.18.1
  5. nvm use 14.18.1
  6. nvm uninstall 14.18.1
  7. nvm alias default 14.18.1
  8. // 同样地,需要配置 npm
  9. npm config set prefix "/opt/node/npm_prefix" // npm 全局安装路径的前缀
  10. npm config set cache "/opt/node/npm_cache" // npm 包缓存位置
  11. npm config set registry "http://registry.npm.taobao.org" // npm 源配置地址

细节

有时下载失败,报错 curl: (60) Peer’s Certificate has expired.,解决办法同步时间:
/usr/sbin/ntpdate ntp.aliyun.com

参考文档:
https://www.cnblogs.com/GManba/p/12861125.html
https://github.com/nvm-sh/nvm