npm+yarn用法(详细篇)
## node
### 安装 node
安装(推荐:LTS)
> https://nodejs.org/en/
查询版本
> node -v
> npm -v
### nvm
> https://github.com/coreybutler/nvm-windows
列出可安装 node 版本
> nvm list available
安装指定的版本(例如:16.13.0)
> nvm install 版本号
列出已安装的版本
> nvm list
使用指定的版本(例如:16.13.0)
> nvm use 版本号
## npm
> ~/.npmrc
### 修改 npm 环境变量
D:\Program Files\nodejs
> 创建 2 个文件夹 node_global 和 node_cache
执行命令
> npm config set prefix "D:\Program Files\nodejs\node_global"
> npm config set cache "D:\Program Files\nodejs\node_cache"
在 node_global 下创建 node_modules 文件夹
系统变量-新建
> 变量名:NODE_PATH
> 变量值:D:\Program Files\nodejs\node_global\node_modules
用户变量
> C:\Users\beyondware\AppData\Roaming\npm
改成
> D:\Program Files\nodejs\node_global
### 安装 cnpm
> npm install -g cnpm --registry=https://registry.npmmirror.com
查询版本
> cnpm -v
### 修改 npm 源
查看当前 npm 源
> npm config get registry
修改 npm 源
> npm config set registry https://registry.npmmirror.com/
还原 npm 源
> npm config set registry https://registry.npmjs.org/
临时修改
> npm install 包名 --registry=https://registry.npmmirror.com
查看配置列表
> npm config list
### nrm
npm
> https://github.com/Pana/nrm
安装 nrm
> npm install -g nrm
列出 nrm 源
> nrm ls
使用 taobao
> nrm use taobao
测试响应速度
> nrm test taobao
添加镜像地址
> nrm add URL 'registry_name'
删除镜像地址
> nrm del 'registry_name'
### npm 常用命令
初始化
> npm init
> npm init -y //全部 yes
安装
> npm install
> npm i
> npm install -dev //开发环境
> npm install -g //全局安装
安装指定版本
> npm install 包名@版本号
> npm install 包名@latest
删除
> npm uninstall
升级
> npm update
清空缓存
> npm cache clean --force
## yarn
> ~/.yarnrc
### 修改 yarn 源
查看当前 yarn 源
> yarn config get registry
修改 yarn 源
> yarn config set registry https://registry.npmmirror.com/
还原 yarn 源
> yarn config set registry https://registry.yarnpkg.com/
临时修改
> yarn save 包名 --registry https://registry.npmmirror.com/
### 修改 yarn 环境变量
查看 yarn 版本
> yarn -v
创建 yarn_global 文件夹(全局安装)
> yarn config set global-folder "D:\Program Files\Yarn\yarn_global"
创建 yarn_cache 文件夹(全局缓存)
> yarn config set cache-folder "D:\Program Files\Yarn\yarn_cache"
> yarn config set prefix "D:\Program Files\Yarn\yarn_global"
查看所有配置
> yarn config list
查看当前 bin 位置
> yarn global bin
添加到系统环境变量
> D:\Program Files\Yarn\yarn_global\bin
查看当前全局安装位置
> yarn global dir
### yrm
> https://github.com/i5ting/yrm
安装 yrm
> npm install -g yrm
列出 yrm 源
> yrm ls
使用 taobao
> yrm use taobao
测试访问速度
> yrm test taobao
### cgr
cgr:npm、yarn
> https://github.com/daysai/cgr
> https://www.npmjs.com/package/cgr
### yarn 常用命令
初始化
> yarn init
安装所有包
> yarn install
添加依赖项
> yarn add
> yarn add --dev //开发环境
> yarn global add //全局安装
删除
> yarn remove
升级
> yarn upgrade
> yarn up
列出已安装
> yarn list
缓存列表
> yarn cache list
清空缓存
> yarn cache clean