如何升级node到指定的版本?

12,156 阅读1分钟

一.背景

  在启动某项目时,遇到了如下的问题,经查询是因为node版本号过低导致的,那该如何把node版本号升级呢? image.png

image.png

二.解决流程

2.1 清除npm缓存

npm cache clean -f

2.2 安装node的版本管理工具n

npm install n -g

2.3 查看node所有版本

npm view node versions

image.png

2.4 升级node版本

# 升级到指定的版本号
n 16.0.0

# 升级到最新的稳定版本
n stable

# 升级到最新的版本
n latest

2.5 清除shell命令缓存(如果没有这一步,执行node -v时,看到的还是老版本号)

# 清除命令缓存,相当于重开了个shell窗口
hash -r

2.6 查看版本号

node -v

image.png

三.总结

  总体来说还是比较简单,中间可能需要注意的就是hash这个命令,详情使用见hash命令的讲解