在 Ubuntu 上配置 Node.js,通常包含安装 Node.js / npm、版本管理、以及基础环境配置。下面按常见场景给你一套清晰步骤。
# 以 Node.js 20 为例
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
验证:
node -v
npm -v
sudo apt update
sudo apt install nodejs npm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install --lts
nvm use --lts
npm config set registry https://registry.npmmirror.com
查看:
npm config get registry
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
添加到 ~/.bashrc:
export PATH=~/.npm-global/bin:$PATH
source ~/.bashrc
mkdir myapp && cd myapp
npm init -y
npm install express
npm install -g pm2
pm2 start app.js
pm2 startup
pm2 save
如果你是想:
可以告诉我你的具体用途,我给你更针对性的配置方案。