nvm le permite instalar y usar diferentes versiones de NodeJS usando la línea de comandos. nvm funciona en cualquier shell POSIX compatible (sh, dash, ksh, zsh, bash), y en Sistemas Operativos UNIX (Tipos UNIX), macOS y Windows WSL.
Instalar
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
En caso de que no tenga CURL instalado puede usar la variante wget
$ wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
Actualizar el PATH
Para actualizar el PATH ejecute un:
$ source .bashrc
o
$ source .bash_profile
También puede hacer un logout/login
Instalar NodeJS
Versión 18 (LTS)
$ nvm install 18
Downloading and installing node v18.15.0...
Downloading https://nodejs.org/dist/v18.15.0/node-v18.15.0-linux-x64.tar.xz...
--2023-04-05 21:48:25-- https://nodejs.org/dist/v18.15.0/node-v18.15.0-linux-x64.tar.xz
Resolving nodejs.org (nodejs.org)... 104.20.22.46, 104.20.23.46, 2606:4700:10::6814:162e, ...
Connecting to nodejs.org (nodejs.org)|104.20.22.46|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 23637576 (23M) [application/x-xz]
Saving to: ‘/home/vagrant/.nvm/.cache/bin/node-v18.15.0-linux-x64/node-v18.15.0-linux-x64.tar.xz’
/home/vagrant/.nvm/.cache/bin/node-v18.15.0-lin 100%[======================================================================================================>] 22.54M 33.6MB/s in 0.7s
2023-04-05 21:48:25 (33.6 MB/s) - ‘/home/vagrant/.nvm/.cache/bin/node-v18.15.0-linux-x64/node-v18.15.0-linux-x64.tar.xz’ saved [23637576/23637576]
Computing checksum with sha256sum
Checksums matched!
Now using node v18.15.0 (npm v9.5.0)
Comprobar la versión
$ node -v
v18.15.0
Versión 19
$ nvm install 19
Downloading and installing node v19.8.1...
Downloading https://nodejs.org/dist/v19.8.1/node-v19.8.1-linux-x64.tar.xz...
--2023-04-05 21:48:45-- https://nodejs.org/dist/v19.8.1/node-v19.8.1-linux-x64.tar.xz
Resolving nodejs.org (nodejs.org)... 104.20.22.46, 104.20.23.46, 2606:4700:10::6814:162e, ...
Connecting to nodejs.org (nodejs.org)|104.20.22.46|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 24209632 (23M) [application/x-xz]
Saving to: ‘/home/vagrant/.nvm/.cache/bin/node-v19.8.1-linux-x64/node-v19.8.1-linux-x64.tar.xz’
/home/vagrant/.nvm/.cache/bin/node-v19.8.1-linu 100%[======================================================================================================>] 23.09M 35.6MB/s in 0.6s
2023-04-05 21:48:46 (35.6 MB/s) - ‘/home/vagrant/.nvm/.cache/bin/node-v19.8.1-linux-x64/node-v19.8.1-linux-x64.tar.xz’ saved [24209632/24209632]
Computing checksum with sha256sum
Checksums matched!
Now using node v19.8.1 (npm v9.5.1)
Comprobar la versión
$ node -v
v19.8.1
Activar versión
Para activar una versión especifica ejecutamos:
$ nvm use version
ejemplo:
$ nvm use 18
Ayuda
$ nvm
Node Version Manager (v0.39.3)
Note: <version> refers to any version-like string nvm understands. This includes:
- full or partial version numbers, starting with an optional "v" (0.10, v0.1.2, v1)
- default (built-in) aliases: node, stable, unstable, iojs, system
- custom aliases you define with `nvm alias foo`
Any options that produce colorized output should respect the `--no-colors` option.
Usage:
nvm --help Show this message
--no-colors Suppress colored output
nvm --version Print out the installed version of nvm
nvm install [<version>] Download and install a <version>. Uses .nvmrc if available and version is omitted.
The following optional arguments, if provided, must appear directly after `nvm install`:
-s Skip binary download, install from source only.
-b Skip source download, install from binary only.
--reinstall-packages-from=<version> When installing, reinstall packages installed in <node|iojs|node version number>
--lts When installing, only select from LTS (long-term support) versions
--lts=<LTS name> When installing, only select from versions for a specific LTS line
--skip-default-packages When installing, skip the default-packages file if it exists
--latest-npm After installing, attempt to upgrade to the latest working npm on the given node version
--no-progress Disable the progress bar on any downloads
--alias=<name> After installing, set the alias specified to the version specified. (same as: nvm alias <name> <version>)
--default After installing, set default alias to the version specified. (same as: nvm alias default <version>)
nvm uninstall <version> Uninstall a version
nvm uninstall --lts Uninstall using automatic LTS (long-term support) alias `lts/*`, if available.
nvm uninstall --lts=<LTS name> Uninstall using automatic alias for provided LTS line, if available.
nvm use [<version>] Modify PATH to use <version>. Uses .nvmrc if available and version is omitted.
The following optional arguments, if provided, must appear directly after `nvm use`:
--silent Silences stdout/stderr output
--lts Uses automatic LTS (long-term support) alias `lts/*`, if available.
--lts=<LTS name> Uses automatic alias for provided LTS line, if available.
nvm exec [<version>] [<command>] Run <command> on <version>. Uses .nvmrc if available and version is omitted.
The following optional arguments, if provided, must appear directly after `nvm exec`:
--silent Silences stdout/stderr output
--lts Uses automatic LTS (long-term support) alias `lts/*`, if available.
--lts=<LTS name> Uses automatic alias for provided LTS line, if available.
nvm run [<version>] [<args>] Run `node` on <version> with <args> as arguments. Uses .nvmrc if available and version is omitted.
The following optional arguments, if provided, must appear directly after `nvm run`:
--silent Silences stdout/stderr output
--lts Uses automatic LTS (long-term support) alias `lts/*`, if available.
--lts=<LTS name> Uses automatic alias for provided LTS line, if available.
nvm current Display currently activated version of Node
nvm ls [<version>] List installed versions, matching a given <version> if provided
--no-colors Suppress colored output
--no-alias Suppress `nvm alias` output
nvm ls-remote [<version>] List remote versions available for install, matching a given <version> if provided
--lts When listing, only show LTS (long-term support) versions
--lts=<LTS name> When listing, only show versions for a specific LTS line
--no-colors Suppress colored output
nvm version <version> Resolve the given description to a single local version
nvm version-remote <version> Resolve the given description to a single remote version
--lts When listing, only select from LTS (long-term support) versions
--lts=<LTS name> When listing, only select from versions for a specific LTS line
nvm deactivate Undo effects of `nvm` on current shell
--silent Silences stdout/stderr output
nvm alias [<pattern>] Show all aliases beginning with <pattern>
--no-colors Suppress colored output
nvm alias <name> <version> Set an alias named <name> pointing to <version>
nvm unalias <name> Deletes the alias named <name>
nvm install-latest-npm Attempt to upgrade to the latest working `npm` on the current node version
nvm reinstall-packages <version> Reinstall global `npm` packages contained in <version> to current version
nvm unload Unload `nvm` from shell
nvm which [current | <version>] Display path to installed node version. Uses .nvmrc if available and version is omitted.
--silent Silences stdout/stderr output when a version is omitted
nvm cache dir Display path to the cache directory for nvm
nvm cache clear Empty cache directory for nvm
nvm set-colors [<color codes>] Set five text colors using format "yMeBg". Available when supported.
Initial colors are:
bygre
Color codes:
r/R = red / bold red
g/G = green / bold green
b/B = blue / bold blue
c/C = cyan / bold cyan
m/M = magenta / bold magenta
y/Y = yellow / bold yellow
k/K = black / bold black
e/W = light grey / white
Example:
nvm install 8.0.0 Install a specific version number
nvm use 8.0 Use the latest available 8.0.x release
nvm run 6.10.3 app.js Run app.js using node 6.10.3
nvm exec 4.8.3 node app.js Run `node app.js` with the PATH pointing to node 4.8.3
nvm alias default 8.1.0 Set default node version on a shell
nvm alias default node Always default to the latest available node version on a shell
nvm install node Install the latest available version
nvm use node Use the latest version
nvm install --lts Install the latest LTS version
nvm use --lts Use the latest LTS version
nvm set-colors cgYmW Set text colors to cyan, green, bold yellow, magenta, and white
Note:
to remove, delete, or uninstall nvm - just remove the `$NVM_DIR` folder (usually `~/.nvm`)