Nodejs + Npm + Git + Pnpm 环境配置

1. NodejsNPM(JavaScript开发环境)

1
sudo apt install -y nodejs npm

配置 npm 镜像源

1
2
3
4
5
6
# 临时使用淘宝源安装包
npm install react --registry=https://registry.npmmirror.com
# 全局配置镜像源(永久生效)
npm config set registry https://registry.npmmirror.com
# 查看 npm 镜像源
npm config get registry

安装 Git

1
sudo apt install git -y

配置 Git 的用户信息

1
2
git config --global user.name "ink-kai"
git config --global user.email "09wanyue@gmail.com"

安装PNPM

1
wget -qO- https://get.pnpm.io/install.sh | sh -

配置PNPM镜像源

1
2
3
4
# 全局配置镜像源(永久生效)
pnpm config set registry https://registry.npmmirror.com
# 查看 npm 镜像源
pnpm config get registry

设置包存储路径

pnpm 默认将依赖存储在 ~/.pnpm-store(内容寻址存储,实现跨项目共享)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 全局bin
pnpm config set global-bin-dir ~/environment/pnpm/bin
# 全局缓存
pnpm config set cache-dir ~/environment/pnpm/cache
# 创建pnpm-state.json文件的目录
pnpm config set state-dir ~/environment/pnpm/state
# 全局安装
pnpm config set global-dir ~/environment/pnpm/global
# 全局仓库路径(类似 .git 仓库)
pnpm config set store-dir ~/environment/pnpm/store
# Node.js 存放位置
pnpm config set pnpm-home ~/environment/pnpm

pnpm config set data-dir ~/environment/pnpm/data

启用Shell自动补全

pnpm 支持 Bash、Zsh、Fish 等 Shell 的自动补全,提升开发效率

1
2
3
4
5
6
7

# 生成补全脚本并添加到 Shell 配置文件(以 Zsh 为例)
pnpm completion zsh >> ~/.zshrc
# 立即生效
source ~/.zshrc
# Bash 用户:pnpm completion bash >> ~/.bashrc
# Fish 用户:pnpm completion fish > ~/.config/fish/completions/pnpm.fish

清理冗余存储

pnpm 存储路径可能积累未使用的依赖,定期清理可释放磁盘空间

1
2
3
4
# 清理未被引用的包
pnpm store prune
# 查看存储占用
pnpm store status