MacOS M3 安装nvm以及node.js

文章目录

    • 说明
    • [一、准备工作(安装 Xcode 命令行工具)](#一、准备工作(安装 Xcode 命令行工具))
    • [二、安装 nvm(官方脚本)](#二、安装 nvm(官方脚本))
    • [三、配置环境(M3 Mac 必做)](#三、配置环境(M3 Mac 必做))
      • [3.1 编辑配置文件](#3.1 编辑配置文件)
      • [3.2 在文件末尾添加(如果没有的话):](#3.2 在文件末尾添加(如果没有的话):)
      • [3.3 让配置立即生效](#3.3 让配置立即生效)
    • 四、验证安装
    • [五、用 nvm 安装 Node.js](#五、用 nvm 安装 Node.js)

说明

  1. 为什么不推荐用 Homebrew 装 nvm
    • 官方不推荐nvm 作者明确写了:
      Please note that Homebrew installation is not supported
  2. 权限、路径冲突超多
    • M 系列 Mac 的 brew 路径是 /opt/homebrew
    • nvm 本身是用户级工具,不是系统级
    • 两者混在一起,很容易出现:
      nvm use 切换版本不生效
      node 命令还是指向系统 /brew 版本
      升级 brew 后 nvm 突然失效
  3. nvm 自己就能完美安装,不需要 brew官方脚本一键安装,干净、独立、不污染系统,卸载也方便。
  4. 用 brew 装 nvm,出问题网上很难搜到解决方案99% 的教程都是「官方脚本安装」,你用 brew 装出 bug,排查成本巨高。

一、准备工作(安装 Xcode 命令行工具)

打开终端执行

bash 复制代码
# 安装
xcode-select --install

# 查看是否安装
xcode-select -p
# 输出/Library/Developer/CommandLineTools或者/Applications/Xcode.app/Contents/Developer
# 说明已经安装

# 没有安装会报错
# Can't find the path...
# xcode-select: error: unable to get active developer directory...

二、安装 nvm(官方脚本)

bash 复制代码
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

三、配置环境(M3 Mac 必做)

macOS 默认是 zsh,需要把 nvm 初始化加到 ~/.zshrc:

3.1 编辑配置文件

bash 复制代码
open ~/.zshrc

3.2 在文件末尾添加(如果没有的话):

bash 复制代码
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"

3.3 让配置立即生效

java 复制代码
source ~/.zshrc

四、验证安装

bash 复制代码
nvm --version
出现版本号(如 0.39.7)即成功。

演示

bash 复制代码
Last login: Wed Apr  1 20:29:25 on ttys000


➜  ~ xcode-select -p
/Library/Developer/CommandLineTools
➜  ~ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 16555  100 16555    0     0  26656      0 --:--:-- --:--:-- --:--:-- 26658
=> Downloading nvm from git to '/Users/shihao/.nvm'
=> 正克隆到 '/Users/shihao/.nvm'...
remote: Enumerating objects: 423, done.
remote: Counting objects: 100% (423/423), done.
remote: Compressing objects: 100% (350/350), done.
remote: Total 423 (delta 59), reused 187 (delta 45), pack-reused 0 (from 0)
接收对象中: 100% (423/423), 413.40 KiB | 1.17 MiB/s, 完成.
处理 delta 中: 100% (59/59), 完成.
* (头指针在 FETCH_HEAD 分离)
  master
=> Compressing and cleaning up git repository

=> Appending nvm source string to /Users/shihao/.zshrc
=> Appending bash_completion source string to /Users/shihao/.zshrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

➜  ~ source ~/.zshrc


➜  ~ nvm --version
0.39.7

五、用 nvm 安装 Node.js

bash 复制代码
# 安装最新 LTS(推荐)
nvm install --lts

# 或安装指定版本(如 20)
nvm install 20

# 设为默认版本
nvm alias default 20

# 查看已装版本
nvm ls

# 切换版本
nvm use 18

# 查看所有可安装的 Node.js 版本
nvm ls-remote

# 只看 LTS 稳定版
nvm ls-remote --lts

node -v   # 看当前版本
npm -v    # 看npm版本
相关推荐
2601_961593423 小时前
Rust 开发环境配置繁琐?RustRover 开箱即用搞定编码调试
开发语言·后端·macos·rust
在水一缸4 小时前
拒绝被打扰:深度解析 macOS 进程守护与“反自动启动”攻防战
macos·策略模式·用户体验·进程守护·自动启动·系统权限
烬羽6 小时前
还在手动拼路径、写回调地狱?一文吃透 Node.js 的 path 和 fs
javascript·程序员·node.js
Darling噜啦啦6 小时前
Node.js path 与 fs 模块完全指南:从路径处理到异步文件操作的进化之路
node.js
子兮曰7 小时前
Node.js v26.5.0 深度解读:import Text、流式 ReadableStreamTee、以及隐藏的安全加固浪潮
前端·后端·node.js
REDcker9 小时前
macOS 挂载 Linux 远程目录详解
linux·运维·macos
2601_961593429 小时前
Mac 上搭建Linux环境吗?VMware + CentOS Stream 9 镜像快速部署
linux·运维·ide·macos·centos
万敏9 小时前
TypeScript 入门第一周完整记录:知识点总结 + 踩坑实录
node.js·全栈
陪我去看海11 小时前
受够了 AI 写完代码留下一堆端口?我做了一个端口管理App!
前端·macos·vibecoding