希曼日记

NPM

本页目录 5
  1. 换源
  2. 安装
  3. npm install安装原理
  4. 安装到依赖环境
  5. 安装方式:

NPM

#PackageManage

换源

Shell
npm config set registry http://registry.npmmirror.com

检查是否替换成功

Shell
npm config get registry

Node Package Manager

安装

  1. 不加包名

根据package.json的依赖环境进行安装

Text
npm intsall
  1. 添加包名
Text
npm install <packname>
OR
npm i <packname>

npm install安装原理

安装到依赖环境

  • 生产和开发依赖

关键字: 不加参数,默认值

Shell
npm install <packname> --save-dev

OR

Shell
npm i <packname> -D
  • 开发依赖

关键字: --save-dev

Shell
npm install <packname> --save-dev

OR

Shell
npm i <packname> -D

查看.npmrc位置

Shell
npm config list

编辑.npmrc文件

Shell
npm config edit

查看npm版本

Shell
npm -v

检查当前npm源 切换回npm默认源

Shell
npm get registry

切换到最新的淘宝源:

Text
npm config set registry https://registry.npmmirror.com

使用阿里云镜像源加速 NPM

Text
npm config set registry https://npm.aliyun.com

使用腾讯云镜像源加速 NPM

Text
npm config set registry http://mirrors.cloud.tencent.com/npm/

使用华为云镜像源加速 NPM

Text
npm config set registry https://mirrors.huaweicloud.com/repository/npm/

返回npm 官方原始镜像

Text
npm config set registry https://registry.npmjs.org/

使用那个镜像,只需要 npm config set registry + 对应的镜像网址就好了

Text
npm config set registry

查看当前的镜像源

Text
npm config get registry

安装方式:

全局安装:

需要全局安装的库一般都会自动配置环境变量,通过环境变量去寻找对应的包

全局安装一般都是工具,例如:

  • yarn
  • pnpm
  • webpack
  • typescript
  • gulp
  • rust

误区:

  • 全局安装一般都是工具
  • axios, express,koa, vue,element-plus一般不是全局安装
  • 尽管全局安装了某些库,但并不是完全在项目保证可用