node.js

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > node.js > nodejs指定版本安装

nodejs指定版本安装的正确步骤及常见问题

作者:蓁蓁啊

在当今的软件开发领域,Node.js已成为一种非常流行的开发工具,特别是对于需要处理大量并发请求的应用来说,Node.js的高效性能表现得尤为出色,这篇文章主要介绍了nodejs指定版本安装的正确步骤及常见问题,需要的朋友可以参考下

Node.js 安装指南(Ubuntu/Debian)

为什么不能用 sudo apt install nodejs?因为版本太老了!

问题诊断

错误方式

sudo apt install nodejs npm

问题:

chrome-devtools-mcp 需要:Node.js ≥18

正确方式:使用 NodeSource 官方源

方法1:一键安装(推荐)

# 安装 Node.js 20.x(LTS 版本,推荐)
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs

# 验证安装
node --version  # 应该显示 v20.x.x
npm --version   # 应该显示 v10.x.x

方法2:使用国内镜像(网络不好时)

# 使用阿里云镜像
curl -fsSL https://mirrors.aliyun.com/nodesource/deb_20.x/setup | sudo -E bash -
sudo apt-get install -y nodejs

# 验证安装
node --version
npm --version

升级已有的旧版本

步骤1:检查当前版本

node --version

如果显示 v10.xv12.xv14.xv16.x,需要升级。

步骤2:卸载旧版本

sudo apt remove nodejs npm
sudo apt autoremove

步骤3:安装新版本

# 使用 NodeSource 官方源
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs

# 验证
node --version  # 应该显示 v20.x.x

版本选择

版本状态推荐场景安装命令
Node.js 20.xLTS(长期支持)✅ 生产环境、日常使用setup_20.x
Node.js 22.xCurrent(最新)尝鲜、测试新特性setup_22.x
Node.js 18.xLTS(维护中)兼容性要求高的项目setup_18.x

推荐:使用 Node.js 20.x(LTS)

常见问题

Q1: curl: command not found

sudo apt update
sudo apt install curl

Q2: 安装脚本下载失败

原因: 网络问题或 GFW

解决: 使用国内镜像

# 阿里云镜像
curl -fsSL https://mirrors.aliyun.com/nodesource/deb_20.x/setup | sudo -E bash -
sudo apt-get install -y nodejs

Q3: npm 权限错误

# 问题:npm install -g 提示权限错误
npm install -g some-package
# Error: EACCES: permission denied

# 解决方案1:配置 npm 全局目录(推荐)
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc

# 解决方案2:使用 sudo(不推荐)
sudo npm install -g some-package

Q4: 如何切换 Node.js 版本?

使用 nvm(Node Version Manager):

# 安装 nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
source ~/.bashrc

# 安装多个版本
nvm install 20
nvm install 18

# 切换版本
nvm use 20
nvm use 18

# 设置默认版本
nvm alias default 20

Q5: 卸载 Node.js

# 卸载通过 apt 安装的 Node.js
sudo apt remove nodejs npm
sudo apt autoremove

# 清理 NodeSource 源
sudo rm -f /etc/apt/sources.list.d/nodesource.list
sudo apt update

# 清理用户数据
rm -rf ~/.npm
rm -rf ~/.nvm

验证安装

完整检查清单

# 1. 检查 Node.js 版本
node --version
# 预期:v20.x.x 或更高

# 2. 检查 npm 版本
npm --version
# 预期:v10.x.x 或更高

# 3. 检查 npx
npx --version
# 预期:v10.x.x 或更高

# 4. 测试运行
node -e "console.log('Node.js 工作正常!')"
# 预期:输出 "Node.js 工作正常!"

# 5. 测试 npm
npm --version
# 预期:显示版本号

# 6. 测试全局安装(可选)
npm install -g cowsay
cowsay "安装成功!"
npm uninstall -g cowsay

为什么选择 NodeSource?

对比项Ubuntu 官方源NodeSource 官方源
更新速度❌ 慢(跟随 Ubuntu 发布周期)✅ 快(跟随 Node.js 发布)
版本❌ 老旧(v10-v18)✅ 最新(v18-v22)
安全更新✅ 有✅ 有
稳定性✅ 高✅ 高
官方支持❌ 社区维护✅ Node.js 官方认可

结论:NodeSource 是 Ubuntu/Debian 上安装 Node.js 的最佳方式。

快速命令参考

# 安装 Node.js 20.x(推荐)
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - && sudo apt-get install -y nodejs

# 国内镜像版本
curl -fsSL https://mirrors.aliyun.com/nodesource/deb_20.x/setup | sudo -E bash - && sudo apt-get install -y nodejs

# 升级已有版本
sudo apt remove nodejs npm && sudo apt autoremove && curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - && sudo apt-get install -y nodejs

# 验证安装
node --version && npm --version

参考资源

“Don’t use the distro Node.js. It’s always outdated.”

— 每个遇到过版本问题的开发者

总结

到此这篇关于nodejs指定版本安装的正确步骤及常见问题的文章就介绍到这了,更多相关nodejs指定版本安装内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文