Skip to content

NPM笔记

NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,常见的使用场景有以下几种:

  • 允许用户从NPM服务器下载别人编写的第三方包到本地使用。
  • 允许用户从NPM服务器下载并安装别人编写的命令行程序到本地使用。
  • 允许用户将自己编写的包或命令行程序上传到NPM服务器供别人使用。

npm版本

sh
npm -v #查看当前版本
npm install -g npm #更新到最新版
npm -g install npm@6.8.0 #更新到指定版本
npm cache clean --force #清理缓存数据

npm镜像源

sh
npm config get registry #查看当前镜像源
npm config set registry https://registry.npmmirror.com #全局设置taobao镜像源,原淘宝镜像:https://registry.npm.taobao.org
npm config set registry https://registry.npmjs.org #全局设置npm官方镜像源
npm install -g cnpm #安装cnpm

nrm配置镜像

sh
npm install nrm -g #全局安装nrm
nrm ls #查看
nrm use taobao #使用淘宝镜像

cnpm安装

sh
npm install -g cnpm --registry=https://registry.npmmirror.com #安装cnpm

npm查看包的所有版本号

sh
npm view [包名] version #仅展示最新版
npm view [包名] versions #展示所有版本号
npm view [包名] versions --json #以json格式展示所有版本号

#如下
npm view pjax version
npm view vue versions
npm view vue-cli versions --json

配置代理

配置git代理(基于v2)

sh
#设置代理
git config --global http.proxy http://127.0.0.1:10809
git config --global https.proxy http://127.0.0.1:10809

配置npm代理(基于v2)

sh
#配置代理
npm config set proxy http://127.0.0.1:10809
npm config set https-proxy http://127.0.0.1:10809

#清除代理
npm config rm proxy
npm config rm https-proxy

配置yarn代理(基于v2)

sh
#配置代理
yarn config set proxy http://127.0.0.1:10809
yarn config set https-proxy http://127.0.0.1:10809

#清除代理
yarn config delete proxy
yarn config delete https-proxy

配置composer代理

sh
# windows
set http_proxy=http://127.0.0.1:10809
set https_proxy=http://127.0.0.1:10809

#linux
export http_proxy=http://127.0.0.1:10809
export https_proxy=http://127.0.0.1:10809