暗黑模式
包管理工具
路线工具
常见包管理工具
- npm,Nodejs 官方自带的默认的包管理工具
- yarn, Facebook 出品的包管理工具,旨在解决
npm
安装速度缓慢等问题 - pnpm, 通过软链接方式解决依赖占用磁盘等问题
- bun,
bun
是一站式JS/TS
的工具包,其中自带了包管理工具,旨在以更快的速度替代npm
yarn
pnpm
更多
关于 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
$ 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
$ 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