升级Node.js版本

删除系统自带的nodejs和npm
[root@localhost ~]# node -v
v16.20.2
[root@localhost ~]# npm -v
8.19.4
[root@localhost ~]# yum remove -y nodejs npm
[root@localhost ~]# node -v
-bash: /usr/bin/node: 没有那个文件或目录
[root@localhost ~]# npm -v
-bash: /usr/bin/npm: 没有那个文件或目录
[root@localhost ~]# 
 
 
 
下载Node.js最新LTS版(目前为20.11.0版,且自带npm-10.2.4)
[root@localhost src]# wget https://nodejs.org/dist/v20.11.0/node-v20.11.0-linux-x64.tar.xz
说明①:需要先安装Python3。
说明②:该版本Node.js为免安装版,直接解压即可使用。
说明③:如果使用源码编译安装建议使用GCC-10,且不指定“--prefix”参数。
 

 
无需执行configure命令编译安装,直接把*.tar.xz文件解压,然后把bin目录加入系统环境变量即可
[root@localhost ~]# vim /etc/profile
export PATH=$PATH:/program/node-20.11/bin
[root@localhost ~]#
 
 
 
查看版本号是否正确
[root@localhost ~]# node -v
v20.11.0
[root@localhost ~]# npm -v
10.2.4
[root@localhost ~]#
 
 
 
命令备忘
[root@localhost ~]# npm cache clean -f # 清空缓存
[root@localhost ~]# npm config set registry https://registry.npmmirror.com # 修改源(淘宝)
[root@localhost ~]# npm config set registry https://registry.npmjs.org # 修改源(官方)
[root@localhost ~]# npm config get registry # 查看当前源
[root@localhost ~]# npm install -g 模块名 # 安装模块
[root@localhost ~]# npm uninstall -g 模块名 # 卸载模块

Copyright © 2024 码农人生. All Rights Reserved