温馨提示×

ubuntu中nodejs如何设

小樊
36
2026-10-16 08:16:30
栏目: 编程语言

你的问题“ubuntu中nodejs如何设”可能指几种常见操作。下面我分别说明,你可以看看需要哪一个:


1. 在 Ubuntu 上安装 Node.js

方式一:用 apt(简单,但版本可能较旧)

sudo apt update
sudo apt install nodejs npm

查看版本:

node -v
npm -v

方式二:用 NodeSource(推荐,版本新)

例如安装 Node 20:

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

方式三:用 nvm(推荐开发用,可切换版本)

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install 20
nvm use 20

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

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

查看:

npm config get registry

3. 设置 Node.js 开机启动服务(如用 pm2)

sudo npm install -g pm2
pm2 start app.js
pm2 startup
pm2 save

4. 设置环境变量(如 NODE_ENV)

临时:

export NODE_ENV=production

永久(加入 ~/.bashrc 或 ~/.profile):

echo 'export NODE_ENV=production' >> ~/.bashrc
source ~/.bashrc

如果你说的是**“如何设置 Node.js 的某一个具体配置”**(比如代理、端口、权限、systemd 服务等),可以补充一下具体场景,我给你更精确的命令。

0 踩