Centos安装Node环境

wget https://nodejs.org/dist/latest-v16.x/node-v16.20.2-linux-x64.tar.gz
tar zxvf node-v16.20.2-linux-x64.tar.gz
mv /root/work/node-v16.20.2-linux-x64 /usr/local/
ln -s /usr/local/node-v16.20.2-linux-x64/bin/node /usr/local/bin/node
ln -s /usr/local/node-v16.20.2-linux-x64/bin/npm /usr/local/bin/npm

# 定义要添加的环境变量
NODE_HOME_LINE="export NODE_HOME=/usr/local/node-v16.20.2-linux-x64/bin"
PATH_LINE="export PATH=\$PATH:\$NODE_HOME:/usr/local/bin"

# 检查 /etc/profile 是否已经包含这些行,如果没有,则追加
grep -qxF "$NODE_HOME_LINE" /etc/profile || echo "$NODE_HOME_LINE" | sudo tee -a /etc/profile
grep -qxF "$PATH_LINE" /etc/profile || echo "$PATH_LINE" | sudo tee -a /etc/profile

source /etc/profile
copy success
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

参考 (opens new window)