在 CentOS 上设置 Node.js 的路径,通常有以下几种常见场景和方法。你可以根据自己是临时使用、用户级配置还是系统级配置来选择。
which node
node -v
/usr/bin/node 或 /usr/local/bin/node)curl -sL https://rpm.nodesource.com/setup_18.x | bash -
yum install -y nodejs
dnf install -y nodejs
安装后 Node 一般在:
/usr/bin/node
/usr/bin/npm
✅ 这种方式的路径已自动加入 PATH,无需手动设置。
wget https://nodejs.org/dist/v18.19.0/node-v18.19.0-linux-x64.tar.xz
tar -xf node-v18.19.0-linux-x64.tar.xz -C /opt
export PATH=/opt/node-v18.19.0-linux-x64/bin:$PATH
vi ~/.bashrc
添加:
export PATH=/opt/node-v18.19.0-linux-x64/bin:$PATH
保存后执行:
source ~/.bashrc
vi /etc/profile
添加:
export PATH=/opt/node-v18.19.0-linux-x64/bin:$PATH
执行:
source /etc/profile
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install 18
nvm use 18
✅ nvm 会自动管理 Node 路径,不需要手动配置 PATH。
which node
node -v
npm -v
node: command not foundsource ~/.bashrc)which -a node
删除或调整 PATH 顺序即可。
如果你能告诉我:
我可以给你更精确的配置方案。