一、Node.js 环境配置
1. 安装 Node.js 和 npm
推荐方式:通过 nvm
管理多版本 Node.js
# 安装 nvm(Node Version Manager)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
# 重启终端后验证安装
nvm --version
# 安装最新 LTS 版本 Node.js
nvm install --lts
# 设置默认版本
nvm alias default lts/*
# 验证安装
node -v
npm -v
备选方案:直接安装 Node.js
- 官网下载:Node.js 官网
- 或通过 Homebrew:
brew install node
2. 配置 npm 镜像(国内用户)
# 设置淘宝镜像
npm config set registry https://registry.npmmirror.com
# 可选:安装 yarn 或 pnpm
npm install -g yarn pnpm
yarn config set registry https://registry.npmmirror.com
3. 全局常用工具
npm install -g typescript eslint prettier vite create-react-app
二、Git 环境配置
1. 安装 Git
# 通过 Homebrew 安装最新版
brew install git
# 或使用 Xcode 自带的 Git(需安装 Xcode Command Line Tools)
xcode-select --install
2. 配置 Git 全局信息
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# 其他常用配置
git config --global core.editor "code --wait" # 使用 VSCode 作为编辑器
git config --global init.defaultBranch main # 设置默认分支为 main
3. SSH 密钥配置(连接 GitHub/GitLab)
# 生成 SSH 密钥
ssh-keygen -t ed25519 -C "your.email@example.com"
# 将公钥添加到 GitHub/GitLab
cat ~/.ssh/id_ed25519.pub
4. 安装 Git GUI 工具(可选)
三、基础开发工具安装
1. Homebrew(包管理器)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# 添加 Homebrew 到 PATH(M1/M2 芯片需额外配置)
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zshrc
source ~/.zshrc
2. 代码编辑器
-
VSCode(推荐):
brew install --cask visual-studio-code
安装常用插件:ESLint、Prettier、Volar、Live Server、GitLens。
-
WebStorm(JetBrains 付费 IDE):
brew install --cask webstorm
3. 浏览器调试工具
- Chrome / Firefox Developer Edition
- 安装 React/Vue 开发者工具扩展
四、其他实用工具
1. 终端增强
- iTerm2(替代默认终端):
brew install --cask iterm2
- Oh My Zsh(Shell 美化):
推荐插件:sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
git
,zsh-autosuggestions
,zsh-syntax-highlighting
2. Docker(可选)
brew install --cask docker
3. 数据库工具(可选)
- TablePlus / DBeaver / MongoDB Compass
五、验证环境
- 创建测试项目:
mkdir test-project && cd test-project npm init -y git init echo "console.log('Hello World')" > index.js
- 运行测试:
node index.js git add . && git commit -m "Initial commit"
常见问题
- 权限问题:避免使用
sudo
安装 npm 包,用nvm
或修复权限:sudo chown -R $(whoami) ~/.npm
- M1/M2 芯片兼容性:大多数工具已适配,如遇问题可通过 Rosetta 运行:
arch -x86_64 zsh # 临时切换到 x86 模式
完成以上步骤后,你的 macOS 已配置好现代化的前端开发环境! 🎉