Ubuntu 22.04安装Node.js

一般情况下,可以使用以下指令安装:
sudo apt install nodejs npm
但是安装完的版本太低:

$ nodejs --version
v12.22.9

这里使用第三方源安装,首先添加源:
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo bash -
这里setup_lts.x可以被替换例如setup_16.x等来指定不同的Node.js版本,
在安装之前可能需要事先安装编译工具:
sudo apt install gcc g++ make
(如果不确定是否已安装,可以执行一遍)
随后使用
sudo apt install nodejs
来安装Node.js以及其包管理工具NPM
使用指令确认安装

$ node --version
v18.12.1
$ npm --version
8.19.2

安装完毕切换NPM镜像源

在安装时临时使用
npm --registry https://registry.npm.taobao.org install xxx
替换默认镜像源(此处为淘宝源)
npm config set registry https://registry.npm.taobao.org
官方源:https://registry.npmjs.org/
查看当前镜像源
npm config get registry