debian

Debian上如何更新Node.js版本

小樊
38
2025-02-19 22:17:39
栏目: 编程语言

在Debian系统上更新Node.js版本,你可以选择使用NodeSource存储库或者使用nvm(Node Version Manager)。下面是两种方法的详细步骤:

方法1:使用NodeSource存储库

  1. 首先,删除已安装的Node.js(如果有的话):
sudo apt-get remove --purge nodejs
sudo apt-get autoremove
  1. 更新系统包列表:
sudo apt-get update
  1. 安装NodeSource存储库。以下命令将添加Node.js 14.x的存储库(你可以根据需要选择其他版本):
curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -
  1. 安装Node.js:
sudo apt-get install -y nodejs
  1. 验证Node.js和npm版本:
node --version
npm --version

方法2:使用nvm(Node Version Manager)

  1. 首先,卸载已安装的Node.js(如果有的话):
sudo apt-get remove --purge nodejs
sudo apt-get autoremove
  1. 安装nvm。运行以下命令以安装nvm:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

或者

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
  1. 激活nvm。在你的shell配置文件(如~/.bashrc~/.zshrc等)中添加以下行:
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

然后运行以下命令以使更改生效:

source ~/.bashrc
  1. 使用nvm安装最新版本的Node.js:
nvm install node
  1. 验证Node.js和npm版本:
node --version
npm --version

现在你已经成功地在Debian系统上更新了Node.js版本。如果你想切换到其他版本,只需使用nvm install <version>nvm use <version>命令。

0
看了该问题的人还看了