你是想在 Linux 上安装并配置 Node.js 环境吧?下面给你几种最常见、实用的做法,按推荐程度排序。
适用于 Ubuntu / Debian / CentOS 等。
sudo apt update
sudo apt install -y curl
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
node -v
npm -v
适合多版本切换、本地开发。
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
重新打开终端或执行:
source ~/.bashrc
安装 Node:
nvm install 20
nvm use 20
sudo apt install nodejs npm
sudo yum install nodejs npm
⚠️ 版本通常较老,不建议生产环境使用。
npm config set registry https://registry.npmmirror.com
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
mkdir test && cd test
npm init -y
node -e "console.log('Node is working')"
如果你说的是**“用 Node.js 配 XX 服务”**(比如 Nginx、PM2、Docker、反向代理等),告诉我具体场景,我可以直接给你完整配置方案。