温馨提示×

怎么在linux用nodejs配

小樊
34
2026-10-27 23:46:38
栏目: 编程语言

你是想在 Linux 上安装并配置 Node.js 环境吧?下面给你几种最常见、实用的做法,按推荐程度排序。


一、推荐方式:用 NodeSource 安装(版本新、稳定)

适用于 Ubuntu / Debian / CentOS 等。

1. 更新系统

sudo apt update
sudo apt install -y curl

2. 添加 NodeSource 源(以 Node 20 为例)

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

3. 安装 Node.js

sudo apt install -y nodejs

4. 验证

node -v
npm -v

二、用 NVM 安装(推荐开发者使用)

适合多版本切换、本地开发。

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

重新打开终端或执行:

source ~/.bashrc

安装 Node:

nvm install 20
nvm use 20

三、用系统包管理器(最简单但版本旧)

Ubuntu / Debian

sudo apt install nodejs npm

CentOS

sudo yum install nodejs npm

⚠️ 版本通常较老,不建议生产环境使用。


四、基础配置(可选但常用)

设置 npm 国内镜像(快)

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

全局目录(避免 sudo)

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、反向代理等),告诉我具体场景,我可以直接给你完整配置方案。

0 踩