乐闻世界logo
搜索文章和话题

How to properly upgrade node using nvm

1个答案

1

First, nvm (Node Version Manager) is a tool for managing multiple Node.js versions, enabling users to easily switch between and install different versions. The following steps detail how to upgrade Node.js using nvm:

  1. Install or Verify nvm Installation: Before upgrading Node.js with nvm, ensure it is installed on your system. Verify this by running the command in your terminal:
    shell

nvm --version

shell
If nvm is not installed, consult the official [nvm GitHub repository](https://github.com/nvm-sh/nvm) for installation instructions. 2. **List Available Node.js Versions**: To check available Node.js versions, use nvm to list all remote versions:

nvm ls-remote

shell
This command displays all available versions, including Long-Term Support (LTS) releases. 3. **Install a New Node.js Version**: When installing a specific version, use:

nvm install

shell
Replace `<version>` with a specific version number (e.g., `14.17.0`) or use `node` to install the latest stable version:

nvm install node

shell
This installs the most recent stable Node.js release. 4. **Switch to the New Version**: After installation, switch to the new version with:

nvm use

shell
Similarly, substitute `<version>` with the installed version or use `node` to select the latest version. 5. **Verify the New Node.js Version**: Confirm the current version after completing the steps:

node --version

shell
This should display the selected version. 6. **Upgrade npm (if needed)**: Occasionally, upgrade npm (Node Package Manager) using:

npm install -g npm

shell
7. **Set the Default Node.js Version**: To use the new version in all new terminal sessions, set it as the default:

nvm alias default

shell
8. **Reinstall Global Packages if Necessary**: After upgrading Node.js, you may need to reinstall global packages. Do this by checking current global packages and reinstalling them in the new version:

nvm ls-remote --reinstall-packages-from=<old_version>

shell
Replace `<old_version>` with the previous Node.js version you used. By following these steps, you can safely and correctly upgrade Node.js using nvm without disrupting older versions or their dependencies.
2024年6月29日 12:07 回复

你的答案