暗黑模式
包管理工具
路线工具
常见包管理工具
- npm,Nodejs 官方自带的默认的包管理工具
- yarn, Facebook 出品的包管理工具,旨在解决
npm
安装速度缓慢等问题 - pnpm, 通过软链接方式解决依赖占用磁盘等问题
- bun,
bun
是一站式JS/TS
的工具包,其中自带了包管理工具,旨在以更快的速度替代npm
yarn
pnpm
初始化项目
安装项目所有依赖包
bash
npm install
npm install --production # npm will not install modules listed in devDependencies.
npm install --production=false # Install all modules listed in both dependencies and devDependencies when NODE_ENV environment variable is set to production.
npm config set registry https://registry.npmmirror.com
1
2
3
4
2
3
4
bash
# First
corepack enable
corepack use pnpm@latest # Wll add `packageManager: pnpm@x.x.x` to package.json
pnpm -v
pnpm install
# Other machine
corepack enable
pnpm -v
pnpm install
# Registry
pnpm config set registry https://registry.npmmirror.com
# Node version manager
pnpm env use --global lts
pnpm env use --global 22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bash
yarn
yarn install
yarn install --production
yarn install --prod
yarn install --production=false
yarn install --prod=false
yarn config set registry https://registry.npmmirror.com
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
bash
# nodejs 18+
corepack enable # only once
yarn set version berry # will add `packageManager: yarn@4.x.x` to package.json; generate .yarn folder, .yarnrc.yml
yarn -v # output: 4.x.x
yarn
yarn install
yarn config set npmRegistryServer https://registry.npmmirror.com # optional
# .gitignore: add this line to ignore .yarn folder
.yarn
# other machine
corepack enable
corepack install # will download package manager according to `packageManager` in package.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bash
bun install [--production]
1
安装指定依赖包
bash
npm install <pkg-name>
npm install --global <pkg-name>
npm install -g <pkg-name>
# aliases: add, i, in, ins, inst, insta, instal, isnt, isnta, isntal, isntall
# -P, --save-prod: Package will appear in your dependencies. This is the default unless -D or -O are present.
# -D, --save-dev: Package will appear in your devDependencies.
# -O, --save-optional: Package will appear in your optionalDependencies.
# --no-save: Prevents saving to dependencies.
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
bash
yarn add <pkg-name>
yarn global add <pkg-name>
# This will install one or more packages in your dependencies.
# Using --dev or -D will install one or more packages in your devDependencies.
# Using --peer or -P will install one or more packages in your peerDependencies.
# Using --optional or -O will install one or more packages in your optionalDependencies.
1
2
3
4
5
6
7
2
3
4
5
6
7
bash
bun add
bun add [--dev,development | -d,D] # devDependencies
bun add --optional # optionalDependencies
bun add [--exact | -E]
bun add [--global | -g]
bun add --help
1
2
3
4
5
6
2
3
4
5
6
列出已安装的包
bash
npm ls # 列出当前项目已经安装的依赖包
npm list # 同上,list 是 ls 的别名
npm ls --global # 列出全局安装的包
/home/ronnie/.nvm/versions/node/v20.12.0/lib
├── corepack@0.25.2
└── npm@10.5.0
1
2
3
4
5
6
7
2
3
4
5
6
7
bash
yarn list # 列出当前项目已经安装的依赖包
yarn global list # 列出全局安装的包
yarn global v1.22.19
info "nodemon@3.1.0" has binaries:
- nodemon
Done in 0.11s.
1
2
3
4
5
6
7
2
3
4
5
6
7
bash
bun pm ls [--all]
1
问题排查
ERR_PNPM_FETCH_404
原因之一:设置了代理镜像,代理镜像没有及时同步 npm 源。
解决方法:
- 浏览器中:https://npmmirror.com/sync/{some-package}
- 在页面中找到【进行同步】按钮并点击,等待片刻,然后
pnpm install