温馨提示×

debian如何配置nodejs

小樊
37
2026-09-14 11:18:21
栏目: 编程语言

在 Debian 上配置 Node.js 常见有 三种方式
1️⃣ 使用 Debian 官方源(最简单,但版本较旧)
2️⃣ 使用 NodeSource(推荐,版本新且稳定)
3️⃣ 使用 nvm(适合开发,多版本切换)

下面分别说明。


一、使用 Debian 官方仓库(不推荐生产)

sudo apt update
sudo apt install -y nodejs npm

查看版本:

node -v
npm -v

⚠️ Debian 11/12 中的 Node 版本通常较旧(如 12/18)。


二、使用 NodeSource(✅ 推荐)

Node.js 20 LTS 为例:

sudo apt update
sudo apt install -y curl ca-certificates gnupg

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs

验证:

node -v
npm -v

✅ 优点:

  • 版本新
  • 适合生产环境
  • 更新方便

三、使用 nvm(适合开发)

1. 安装 nvm

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

重新登录或执行:

source ~/.bashrc

2. 安装 Node

nvm install 20
nvm use 20

3. 设置默认版本

nvm alias default 20

✅ 优点:

  • 多版本切换
  • 不影响系统 Node

四、常见配置(可选)

1. 设置 npm 镜像(国内加速)

npm config set registry https://registry.npmmirror.com

2. 全局模块目录(避免权限问题)

mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc

五、验证是否成功

node -e "console.log('Node is working')"

如果你告诉我:

  • Debian 版本(cat /etc/os-release
  • 用途(开发 / 生产 / 服务器部署)

我可以给你更精确的配置方案。

0