npm 与 yarn 常用命令对比

  1. 初始化项目
1
2
npm  init
yarn init
  1. 从 package.json 安装依赖
1
2
npm install
yarn
  1. 安装指定包到依赖或者开发依赖
1
2
3
4
npm install --save [package]
yarn add [package]
npm install --save-dev [package]
yarn add [package] [--dev/-D]
  1. 安装包到全局
1
2
npm install --global [package]
yarn global add [package]
  1. 安装指定版本的包
1
2
npm install [package]@[version]
yarn add [package]@[version]
  1. 重新下载所有包
1
2
npm rebuild
yarn install --force
  1. 卸载包
1
2
3
4
npm uninstall [package]
npm uninstall --save [package]
yarn remove [package]
npm uninstall --save-dev [package]
  1. 升级包
1
2
rm -rf node_modules && npm install
yarn upgrade

逐一对比:

npmyarn
npm installyarn install
(N/A)yarn install –flat
(N/A)yarn install –har
(N/A)yarn install –no-lockfile
(N/A)yarn install –pure-lockfile
npm install [package](N/A)
npm install –save [package]yarn add [package]
npm install –save-dev [package]yarn add [package][–dev/-D]
(N/A)yarn add [package][–peer/-P]
npm install –save-optional [package]yarn add [package][–optional/-O]
npm install –save-exact [package]yarn add [package][–exact/-E]
(N/A)yarn add [package][–tilde/-T]
npm install –global [package]yarn global add [package]
npm rebuildyarn install –force
npm uninstall [package](N/A)
npm uninstall –save [package]yarn remove [package]
npm uninstall –save-dev [package]yarn remove [package]
npm uninstall –save-optional [package]yarn remove [package]
npm cache cleanyarn cache clean
rm -rf node_modules && npm installyarn upgrade