npm和yarn常用命令

7/25/2021 npmyarn

npm i 等同于 npm install
--save 等同于 -S
--save-dev 等同于 -D

dependencies 是运行时的依赖, devDependencies 是开发时的依赖。

安装 yarn: npm install yarn -g

npm yarn
npm install yarn
npm install react --save yarn add react
npm uninstall react --save yarn remove react
npm install react --save-dev yarn add react --dev
npm update --save yarn upgrade

# package.json 文件中版本号含义解释

"5.0.3",
"~5.0.3",
"^5.0.3"
1
2
3

“5.0.3”表示安装指定的 5.0.3 版本,“~ 5.0.3”表示安装 5.0.X 中最新的版本,“^5.0.3”表示安装 5.X.X 中最新的版本。

# 查看全局安装的包

npm:

npm list -g --depth 0
1

yarn:

yarn global list
1

# 查看和修改的镜像源

npm:

# 查询当前镜像
npm get registry

# 设置为淘宝镜像
npm config set registry https://registry.npm.taobao.org

# 设置回官方的镜像
npm config set registry https://registry.npmjs.org
1
2
3
4
5
6
7
8

yarn:

# 查询当前镜像
yarn config get registry

# 设置为淘宝镜像
yarn config set registry https://registry.npm.taobao.org

# 设置回默认的官方镜像
yarn config set registry https://registry.yarnpkg.com
1
2
3
4
5
6
7
8

# 初始化

npm:

npm init
1

yarn:

yarn init
1

# 安装 package.json 里面的依赖包

如果 node_modules 中有相应的包则不会重新下载 --force 可以强制重新下载安装 npm:

npm install
# 强制下载安装
npm install --force
1
2
3

yarn:

yarn
# 强制下载安装
yarn --force
1
2
3

# 安装指定版本的包

npm:

# 全局安装
npm install xxx@1.1.1 -g
&&
npm i xxx@1.1.1 -g

# 安装生产环境依赖的模块,保存在package.json中dependencies字段下
npm install xxx@1.1.1 --save
&&
npm i xxx@1.1.1 -S

# 安装开发环境依赖的模块,保存在package.json中devDependencies字段下
npm install xxx@1.1.1 --save-dev
&&
npm i xxx@1.1.1 -D
1
2
3
4
5
6
7
8
9
10
11
12
13
14

yarn:

# 全局安装
yarn global add xxx@1.1.1

# 安装生产环境依赖的模块,保存在package.json中dependencies字段下
yarn add xxx@1.1.1

# 安装开发环境依赖的模块,保存在package.json中devDependencies字段下
yarn add xxx@1.1.1 --dev
&&
yarn add xxx@1.1.1 -D
1
2
3
4
5
6
7
8
9
10

# 更新包

npm:

# 升级所有依赖项
npm update

# 指定升级哪类依赖
npm update webpack --save-dev
1
2
3
4
5

yarn:

yarn upgrade # 升级所有依赖项,不记录在 package.json 中

yarn upgrade webpack # 升级指定包

yarn upgrade --latest # 忽略版本规则,升级到最新版本,并且更新 package.json
1
2
3
4
5

# 卸载包

npm:

npm uninstall xxx --save(-dev)
1

yarn:

yarn remove xxx
1

补充:

npm uninstall 模块:删除模块,但不删除模块留在package.json中的对应信息

npm uninstall 模块 --save 删除模块,同时删除模块留在 package.json 中 dependencies 下的对应信息
   npm uninstall 模块 --save-dev 删除模块,同时删除模块留在 package.json 中 devDependencies 下的对应信息

# 运行脚本

用来执行在 package.json 中 scripts 属性下定义的脚本 npm:

npm run xxx
1

yarn:

yarn run xxx
&&
yarn xxx
1
2
3

# 管理配置文件

npm:

# 设置
npm config set key value

# 读取值
npm config get key

# 删除
npm config delete key

# 显示当前配置
npm config list

# 设置淘宝镜像
npm config set registry https://registry.npm.taobao.org
1
2
3
4
5
6
7
8
9
10
11
12
13
14

yarn:

# 设置
yarn config set key value

# 读取值
yarn config get key

# 删除
yarn config delete key

# 显示当前配置
yarn config list

# 设置淘宝镜像
yarn config set registry https://registry.npm.taobao.org
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 清除缓存

npm:

npm cache clean -f
&&
npm cache clean --force
1
2
3

yarn:

yarn cache clean
1

# 增加npm权限

npm install --unsafe-perm=true --allow-root
1
上次更新: 2024/09/10 17:37:12
最近更新
01
跨域的几种常见解决方案
04-03
02
react教程
03-01
03
前端抓包神器whistle
09-01