Linux下node最新稳定版环境安装简易版

安装

需要安装的有什么

  • nodejs

  • npm(一般来说是和nodejs绑定的,但也可能要单独安装)

具体步骤

测试是否已安装nodejs:

node -v

npm -v

如果显示 Command 'xxx' not found,则需安装

sudo apt update

sudo apt install nodejs

sudo apt install npm

node -v

npm -v

node -v 输出显示 v10.19.0
npm -v 输出显示 6.14.4
(你的可能不一样)

注意直接 apt install 的这个nodejs版本很老,打开nodejs官网发现最新版都已经16.14了,10.19.0属实是远古版本了

所以我们还要更新nodejs的版本。

这里我使用nodejs的版本管理工具n

npm cache clean -f

sudo npm install -g n

然后用n来安装最新稳定版nodejs

sudo n stable

我这边输出了

   installed : v16.14.0 (with npm 8.3.1)

Note: the node command changed location and the old location may be remembered in your current shell.
         old : /usr/bin/node
         new : /usr/local/bin/node
If "node --version" shows the old version then start a new shell, or reset the location hash with:
hash -r  (for bash, zsh, ash, dash, and ksh)
rehash   (for csh and tcsh)

意思是16.14.0(上面途中左边的LTS稳定版本)已经安装了,但是现在你的 node -v 命令还是输出的老的版本,你要用 hash -r 或者 rehash 来更新一下

更新完之后 node -vv16.14.0, npm -v8.3.1

【番外】npm 换源

可以用淘宝的镜像源

  1. 临时使用
    npm --registry https://registry.npm.taobao.org install express

  2. 持久使用
    npm config set registry https://registry.npm.taobao.org

配置后可通过以下方法验证是否成功

npm config get registry

其实推荐使用这个标准版安装

Linux下node最新稳定版环境安装标准版