- Add `--name` to `docker run` command to make the container easier to
manipulate. (This will produce collisions if two developers both try
this on the same system, but it's not worth a lot of extra
documentation to try to sort out the few developers who can't figure
out what's going on from the error messages)
- Explain better that the current repo and working copy are copied to ~/.nvm/ inside the container
- Explain that the container is not deleted when you exit, and how to re-enter and delete it
- General improvements to the language.
Co-authored-by: Curt J. Sampson <cjs@cynic.net>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
Several "pick a recent version" examples cited EOL Node versions
(4.2, 4.8.3, 5.9, 6.10.3, 8.x, 12.22.1, 14.x, 16.x, 18.x, and Iron's 20).
Update them to current LTS lines (22.x/24.x),
including the full `.nvmrc` walkthrough output,
preserving the `--help` column alignment.
Intentionally-historical references are left unchanged:
the io.js migration examples,
the `stable`/`unstable` alias notes,
the Alpine version table,
the source-build narrative,
and the `--shared-zlib` example
(which explicitly demonstrates installing an older version)
Signed-off-by: Than Tibbetts <than@me.com>
Co-authored-by: Than Tibbetts <than@me.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
`nvm_ls_remote_index_tab` compared the codename with awk's `!~`,
so the name was a regex: `--lts=.` matched every LTS line,
and `--lts=i.on` or `--lts=(` didn't mean what they say.
Use `index()` so a name still matches every codename that contains it,
but literally, and do the same in `nvm_filter_lts_versions`,
for `--lts=<name>` in an alias.
Locally, `--lts=<name>` resolved the `lts/<name>` alias,
which only exists for full codenames,
so `nvm use --lts=ir` failed where `nvm install --lts=ir` installed Iron.
Add `nvm_resolve_lts_name`,
which picks the newest codename in `$NVM_DIR/.lts-status` that contains the name,
and resolve `lts/<codename>` with it in `nvm use`, `uninstall`, `exec`, `run`,
and in `nvm install` for its offline lookup and the default and `--alias=` it writes.
Without a recorded status, or when no codename contains the name,
the name is used as given.
Signed-off-by: Divya Chukkapalli <12871391+divya0795@users.noreply.github.com>
Co-authored-by: Divya Chukkapalli <12871391+divya0795@users.noreply.github.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
`install.sh`: an existing install now fetches from the remote whose URL is nvm's source,
adding one when none matches,
instead of always fetching `origin`;
fresh installs still name it `origin`.
The remote's URL is read with `git config`,
since `git remote get-url` needs git 2.7.
`nvm_ls`: `find` lists each version directory's non-hidden subdirectories itself,
so it works under `set -f`,
which disables the globs it was given;
the directories only need to exist.
`nvm_ensure_version_installed`:
the resolved alias was assigned to `VERSION` without declaring it local.
Co-authored-by: dajiaohuang <mikewushuwen@outlook.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
When completing a path containing `../` for `cd`, `pushd`, or `chdir`,
zsh's `_cd` runs `$(cd ${PREFIX%/*} ...)`,
which fires the `chpwd` hook in a subshell,
so `load-nvmrc` ran on each such completion, slowly in large directories.
`compstate` is set only inside completion widgets.
Checking `ZSH_SUBSHELL` instead would also skip deliberate subshells, like `( cd proj && npm test )`,
and checking it along with `WIDGET` would skip any widget's subshell, not just completion's.
Fixes#3606
Once the newest installed version is 1.0 or later, `stable` is that version, so list the installed versions once and take the newest, instead of `nvm_ls stable` listing them three times.
Only 0.x installs (where `stable` is the newest even minor), a user alias named `stable`, and `--lts` filters keep the existing path. A system node, which `nvm_ls` lists last, is skipped.
Part of #3416.
also: `nvm_get_os`: set `NVM_DIR` in the OpenHarmony chroots, and skip them without a glibc x86-64 layout
`sudo` resets the environment, and `nvm.sh` returns early when it cannot detect `NVM_DIR`,
so in the chroots `nvm_get_os` was undefined and every check got an empty result.
The chroot setup needs `ldd` and `/lib64/ld-linux-x86-64.so.2`, which macOS does not have.
Co-authored-by: hqzing <hqzing@outlook.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
The mirror now redirects to HTTPS,
and `ubuntu:22.04` has no CA certificates before its first `apt install`,
so on amd64 every source was ignored and installing packages failed:
Ign:2 https://ubuntu.cs.utah.edu/ubuntu jammy-updates InRelease
W: http://ubuntu.cs.utah.edu/ubuntu/dists/jammy-updates/InRelease: No system certificates available. Try installing ca-certificates.
Use Ubuntu's default sources, as arm64 builds already did
(`ports.ubuntu.com`, which the `sed` did not rewrite);
Docker caches the package layers anyway.
The repo's security policy page resolves relative links from the repository root,
but viewing `.github/SECURITY.md` resolves them from `.github/`,
so no relative link works in both.
Co-authored-by: Aryan Chandavarkar <aryanmc2004@gmail.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
The duplicate-key check kept keys in a space-separated list,
so a key such as `a b` followed by a key `a` or `b` was reported as a duplicate,
unlike in the `nvmrc` package.
Keep the keys newline-separated, since a key cannot contain a newline.
The duplicate-key check used to splice each key into an extended regex,
so distinct keys whose characters are regex syntax collided
(`node.dist` matched `node-dist`, `a+b` matched `ab`),
a key with `(` or `[` printed a grep error,
and a repeated `[x` key was not caught.
e4fcf988 now compares keys literally with a `case` pattern;
cover those cases.
Signed-off-by: Divya Chukkapalli <12871391+divya0795@users.noreply.github.com>
Rejecting only ASCII control characters in the LTS codename still lets through a UTF-8 C1 control
(like U+009B, a CSI) or a bidi override (like U+202E),
which some terminals act on.
A row is now kept only if its codename is `-`, empty, or alias-safe,
which is the same shape the LTS alias step already requires,
and every real codename has.
BusyBox `tr` mishandles `-c` with a character class,
so on Alpine the expected checksum printed as almost all `?`;
an octal range in the C locale works in BusyBox, GNU, and BSD `tr`,
and does not make BSD `tr` abort on invalid UTF-8.
`index.tab` and `SHASUMS256.txt` are untrusted mirror metadata.
An LTS codename (`$10`) carrying ESC/BEL bytes was printed by `nvm ls-remote`,
and a mismatched expected checksum was echoed as received.
Skip `index.tab` rows whose codename has a control character,
next to the existing version check,
and show non-printable bytes of the expected checksum as `?`.
`nvm_check_global_modules` ran `npm --version` and `npm list -g` in `$PWD`,
so the project `.npmrc` applied, eg its `logs-dir` on npm v8.11 through v10.5.
The "npm unavailable" test put its fake npm on a relative `PATH` entry, which no longer resolves from `/`.
These also ran npm in `$PWD`, applying the untrusted project `.npmrc`.
npm v7 through v8.2 apply it even with `-g`,
so its `registry` chose where `nvm install-latest-npm`, `--latest-npm`, `default-packages`, and `reinstall-packages` downloaded globally-installed packages from, and their install scripts then ran.
npm v8.11 through v10.5 honor its `logs-dir` and `cache` for `npm --version`,
so every non-silent `nvm use` created those directories and wrote a debug log there.
Use `nvm_npm`, or `nvm_cd_root` where `xargs` runs npm.
`npm link` still runs in each linked package's own directory.
`nvm debug` ran `npm config get prefix` in `$PWD`, so npm applied the untrusted project `.npmrc`.
Its `logs-dir` chose where npm wrote its debug log,
and npm 11 copies unknown keys into that log verbatim,
so `logs-dir=~/.bashrc.d` plus a `$(...)` key runs a command in the next shell on Fedora-style setups.
Its `userconfig` makes npm 11 echo the lines of any file into the log and into the `nvm debug` output.
`-g` is not enough: npm v7 through v8.2 load the project `.npmrc` even in global mode.
Add `nvm_npm`, which runs npm from `/`, and use it for both npm commands.
`$NVM_CD_FLAGS` is not used, since a zsh parent exports `-q` to bash and sh children.
Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-ZF4UWQNS.
903e3493 moved `$NVM_AUTH_HEADER` out of the downloader's arguments,
but a mirror URL with `user:password@` in it was still one,
so any local user could read the password in `ps` or /proc/<pid>/cmdline
for every index.tab, SHASUMS, and tarball request.
curl now gets such a URL as `url = "..."` in the config it already reads with `--config -`,
with `"` and `\` escaped;
GNU Wget reads it with `--input-file=-`,
including in the same-origin redirect loop.
A URL with whitespace or a control character cannot be one config line, and stays an argument.
BusyBox wget has no way to take a URL other than as an argument; the README says so.
Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-MNDN98RD.
Every download went to the fixed `<archive>.unverified`,
and curl opens its `-o` file only on the first body byte,
so a curl left running by an earlier install of the same version
(past the install lock via `nvm cache clear`, a stale-lock takeover, or a removed lock)
survived the `rm -f`, then opened the same file
and could rewrite it after the checksum passed, while `tar` read it,
and leave it in the cache that `--offline` trusts.
Downloading into a `mktemp -d` directory in the same cache dir
keeps the rename atomic and the file's mode following the umask.
Leftover `.unverified*` paths from interrupted installs are still removed first.
Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-XUXR5LQ4.
The symlink check and the `tee` were two steps,
so anyone who can write `$PWD` could rename a symlink over `.nvmrc` in between,
and `nvm use --save`/`nvm install --save` would truncate any file the user can write.
Write the version in a private `mktemp -d` dir instead, and rename it into `$PWD/`:
a rename replaces a symlink rather than following it,
and moving into the directory (not onto `.nvmrc`) avoids landing inside a symlinked directory.
An existing `.nvmrc` must be a writable regular file, since a rename would also replace a read-only one.
`.nvmrc` is now a new file whose mode comes from the umask.
Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-EXZRTATX.
`nvm_process_nvmrc` tested `[ ! -L ]` and then opened the path again with `cat`,
so another local user who can write the directory
could rename a symlink over `.nvmrc` in between (timed by `cat`'s exec),
and the target's content was printed and passed to `find` and `sed` as a version,
where that user could read it from the process list (67268ebc's check, bypassed).
sh can't open without following symlinks,
so read the file once, from one open,
and when its content is not a version or an existing alias,
show it or pass it on only if one `lstat` (`find -prune -type f -links 1 -inum`)
finds a regular file at the path with the open file's inode and no other links.
Anything else is handled as a symlink.
A hard link to another user's file, where the OS allows one, is refused the same way.
Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-WJUJDPM2.
pdksh (OpenBSD's `/bin/sh`) and mksh have no `printf` builtin,
so every `nvm_echo` ran `/usr/bin/printf` with its text as an argument,
which every local user can read (`ps`, `/proc/<pid>/cmdline`).
That text includes a symlinked `.nvmrc`'s content while it is parsed,
so these shells still exposed it after 3a6403f9.
The zsh `load-nvmrc` and fish `load_nvm` examples passed `$(cat .nvmrc)` to `nvm version` and `nvm install`,
so a `.nvmrc` symlinked to any readable file put that file's content,
a whole one-line file with no `=` needed,
in the arguments of `find`, `sed`, and (for fish) `bass` and `bash`,
which every local user can read,
and tried to install it as a version.
Get the version from `nvm_process_nvmrc` instead, as the bash `cdnvm` example has since c314f1cb.
bass runs its arguments as bash code,
so the fish example also ran `.nvmrc` content like `v20$(touch x)`;
pass the path and version to bass in the environment instead.
Reported by Anthropic's OSS Scanner, alongside vulnerability ANT-2026-GG4I2LOU.
A symlinked `.nvmrc`'s content is never printed (67268ebc),
but it was still parsed with external commands that took part of it as arguments,
which every local user can read (`ps`, `/proc/<pid>/cmdline`):
the duplicate-key check passed each key (the text before a line's first `=`) to `grep -E`,
and `nvm_is_valid_version` passed prerelease-shaped text to `awk` before checking that its core is numeric.
So another local user who can place a `.nvmrc` symlink where the victim runs nvm
(or merely sources `nvm.sh`, with no default alias)
could read, for example, a WireGuard private key the victim's symlink target holds.
Compare keys with `case`, literally rather than as a regex,
and let the existing shell check validate a prerelease's core,
which makes `0.0.N-<prerelease>` valid.
Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-GG4I2LOU.
add CLI option --min=<version>, fallback to env var "NVM_MIN" if set.
The latter two pick the newest match,
which is below the minimum only when every match is,
so they check the resolved version.
`ls-remote` prints its notice on stderr, so the list stays parseable.
Other minor changes:
- fix: disable color in nvm_print_versions tests
- trim leading space in mock output due to eslint errors
- ensure prefixed versions like v18 also work
- remove function ref duplication
- avoid inline initialization for ksh compatibility
- apply `--min=` before checking for an empty list; require a version
Co-authored-by: ryenus <ryenus@gmail.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
Each branch now only records which system binary applies,
and the check runs in the block that lists the system version,
so the patterns that list it are named in one place.
`nvm_ls` lists the system version only for an empty pattern, `v`, or
`system`, but it checked for a system node/io.js for every pattern.
Each check runs `nvm deactivate` in a subshell, and a found system node
also runs `node -v`. So every partial-version lookup, like resolving a
`default` alias of `24` on shell startup, did that work and discarded it.
Refs #3425, #3416.
Signed-off-by: Wei-Lun Li <b84330808@gmail.com>
Check the downloader exit status before parsing index.tab, and report the failed mirror without exposing its credentials. Discard incomplete responses instead of treating them as successful version lists.
Fixes#673.
`--strip-components 1` (and OpenBSD's `-s` equivalent)
rewrites each name after the extracting tar has checked it,
so with tars that have no `openat2` jail
(upstream GNU tar, busybox tar without its traversal protection)
a member named `node-v…//home/user/.bashrc` was written to `/home/user/.bashrc`,
and a hard link to `node-v…//abs/path` linked that file into the install tree,
both silently and with a zero exit.
Extract the archive as-is into a sibling staging directory,
require exactly one top-level entry that is a real directory,
and rename it into place;
dotfiles in the archive are kept.
busybox tar does not contain hard link targets at all
(absolute, `..`, or through an earlier symlink member),
so refuse any extracted file with more than one link.
No node.js or io.js archive contains a hard link.
Previously, every download was refused only after it was fetched,
a verified cached archive was deleted as "broken",
and the source fallback then downloaded a second archive that was refused the same way,
with no message saying what to install.
The empty-computed-sum branch ended with a bare `return`,
whose status is that of the preceding `nvm_err`, which is 0.
Callers treat 0 as "verified",
so an artifact that could not be verified was installed anyway,
and was even moved into the verified cache slot,
violating nvm.sh's own invariant that only a verified archive may sit at ${TARBALL}
(a later `--offline` install would then use it without any verification).
Return 1 so the artifact is rejected, as intended by #1858.
Fixes#1858
Signed-off-by: NUAAwqy <146524024+NUAAwqy@users.noreply.github.com>
Co-authored-by: NUAAwqy <146524024+NUAAwqy@users.noreply.github.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
@@ -288,6 +288,8 @@ If you're running a system without prepackaged binary available, which means you
- [nodist](https://github.com/marcelklehr/nodist)
- [nvs](https://github.com/jasongin/nvs)
**Note:** When using Cygwin, install the `curl` package through [Cygwin's setup program](https://cygwin.com/install.html). Run `command -v curl` in your Cygwin terminal and ensure it points to Cygwin's `curl` (for example, `/usr/bin/curl`), rather than a Windows executable. A Windows version of `curl` may fail to write downloads to Cygwin paths (see [#2515](https://github.com/nvm-sh/nvm/issues/2515)).
**Note:**`nvm` does not support [Fish] either (see [#303](https://github.com/nvm-sh/nvm/issues/303)). Alternatives exist, which are neither supported nor developed by us:
- [bass](https://github.com/edc/bass) allows you to use utilities written for Bash in fish shell
@@ -384,13 +386,13 @@ nvm install node # "node" is an alias for the latest version
To install a specific version of node:
```sh
nvm install 14.7.0 # or 16.3.0, 12.22.1, etc
nvm install 22.11.0 # or 24.0.0, etc
```
To set an alias:
```sh
nvm alias my_alias v14.4.0
nvm alias my_alias v22.11.0
```
Make sure that your alias does not contain any spaces or slashes.
@@ -402,6 +404,13 @@ You can list available versions using `ls-remote`:
nvm ls-remote
```
To hide versions below a given version, use `--min=`, or set `NVM_MIN` to the same kind of version, for example in your shell profile; `--min=` overrides it. `nvm install` and `nvm version-remote` accept it too, and fail rather than select a version below it:
```sh
nvm ls-remote --min=20
nvm ls-remote --only=lts --min=v18.2
```
When no version or `--lts` filter is given, the list also marks the version the `node` alias would install, and every version that one of your own aliases would resolve to. Each kind of annotation always starts in the same column, so the output can be sliced with `cut -c` or similar tools:
```
@@ -426,16 +435,16 @@ nvm run node --version
Or, you can run any arbitrary command in a subshell with the desired version of node:
```sh
nvm exec4.2 node --version
nvm exec22.11 node --version
```
You can also get the path to the executable to where it was installed:
```sh
nvm which 12.22
nvm which 22.11
```
In place of a version pointer like "14.7" or "16.3" or "12.22.1", you can use the following special default aliases with `nvm install`, `nvm use`, `nvm run`, `nvm exec`, `nvm which`, etc:
In place of a version pointer like "22" or "24.0" or "22.11.0", you can use the following special default aliases with `nvm install`, `nvm use`, `nvm run`, `nvm exec`, `nvm which`, etc:
-`node`: this installs the latest version of [`node`](https://nodejs.org/en/)
-`iojs`: this installs the latest version of [`io.js`](https://iojs.org/en/)
@@ -535,12 +544,12 @@ This reinstalls all global packages from `22.20.0` into the currently-active ver
If you've previously downloaded a node version (or it's still in the cache), you can install it without any network access using the `--offline` flag:
```sh
nvm install --offline 14.7.0
nvm install --offline 22.11.0
```
This resolves versions using only locally installed versions and cached downloads. It will not attempt to download anything. This is useful in air-gapped environments, on planes, or when you want to avoid network latency.
A download is only added to the cache (`$NVM_DIR/.cache`) once its checksum has been verified (unless no checksum tool is available, in which case `nvm` warns that it is continuing without verification). `--offline` does not verify cached archives again, so anything placed in the cache by other means is trusted as-is.
A download is only added to the cache (`$NVM_DIR/.cache`) once its checksum has been verified. `--offline` does not verify cached archives again, so anything placed in the cache by other means is trusted as-is.
You can combine `--offline` with `--lts` to install the latest cached LTS version (as long as LTS aliases have been populated by a prior `nvm ls-remote --lts`):
@@ -659,10 +668,10 @@ To set a default Node version to be used in any new shell, use the alias 'defaul
```sh
nvm alias default node # this refers to the latest installed version of node
nvm alias default 18# this refers to the latest installed v18.x version of node
nvm alias default 18.12# this refers to the latest installed v18.12.x version of node
nvm alias default 22# this refers to the latest installed v22.x version of node
nvm alias default 22.11# this refers to the latest installed v22.11.x version of node
nvm alias default --lts node # this refers to the latest installed LTS version of node
nvm alias default --lts=iron 20# this refers to the latest installed v20.x version of node from the "iron" LTS line
nvm alias default --lts=jod 22# this refers to the latest installed v22.x version of node from the "jod" LTS line
```
nvm records the LTS status of every version each time it downloads the list of available versions, so `--lts` aliases know which installed versions are LTS.
@@ -674,7 +683,7 @@ To use a mirror of the node binaries, set `$NVM_NODEJS_ORG_MIRROR`:
To use a mirror of the io.js binaries, set `$NVM_IOJS_ORG_MIRROR`:
@@ -703,7 +712,7 @@ The header is only sent with requests for URLs under a mirror, never to any othe
nvm gives the header to `curl` and to GNU Wget in a config file (for GNU Wget, a private temporary file that also holds a copy of your `~/.wgetrc`), not as a command-line argument, which other users of the same machine could read. Other `wget`s, like BusyBox's, read no config file, so on a machine shared with other users, install `curl` before using `$NVM_AUTH_HEADER`.
`$NVM_AUTH_HEADER` is the recommended way to authenticate to a mirror. Credentials embedded in the mirror URL itself (`https://user:password@host/...`) also work, and nvm prints such URLs with the credentials masked as `***@`.
`$NVM_AUTH_HEADER` is the recommended way to authenticate to a mirror. Credentials embedded in the mirror URL itself (`https://user:password@host/...`) also work, and nvm prints such URLs with the credentials masked as `***@`. Like the header, such a URL is given to `curl` in a config and to GNU Wget on its standard input, not as a command-line argument; other `wget`s, like BusyBox's, only take a URL as an argument, so on a machine shared with other users, install `curl` before putting credentials in a mirror URL.
### Platforms without official binaries
@@ -735,10 +744,10 @@ Neither host is gapless, and both change over time; the `index.tab` at the root
You can create a `.nvmrc` file containing a node version number (or any other string that `nvm` understands; see `nvm --help` for details) in the project root directory (or any parent directory).
Afterwards, `nvm use`, `nvm install`, and `nvm which` will use the version specified in the `.nvmrc` file if no version is supplied on the command line; if no `.nvmrc` is found either, they exit with status `127`. (`nvm exec` and `nvm run` follow the same `.nvmrc` lookup, but currently fall back to the active node if neither resolves — treat that fallback as undefined behavior; pass an explicit version if you need predictable scripting.) If you want the currently active version, pass `current` explicitly (e.g. `nvm which current`) — `current` is not affected by `.nvmrc`.
For example, to make nvm default to the latest 5.9 release, the latest LTS version, or the latest node version for the current directory:
For example, to make nvm default to the latest 22.11 release, the latest LTS version, or the latest node version for the current directory:
```sh
$ echo"5.9" > .nvmrc
$ echo"22.11" > .nvmrc
$ echo"lts/*" > .nvmrc # to default to the latest LTS version
@@ -751,21 +760,21 @@ Then when you run nvm use:
```sh
$ nvm use
Found '/path/to/project/.nvmrc' with version <5.9>
Now using node v5.9.1(npm v3.7.3)
Found '/path/to/project/.nvmrc' with version <22.11>
Now using node v22.11.0(npm v10.9.0)
```
Running nvm install will also switch over to the correct version, but if the correct node version isn't already installed, it will install it for you.
```sh
$ nvm install
Found '/path/to/project/.nvmrc' with version <5.9>
`nvm use` et. al. will traverse directory structure upwards from the current directory looking for the `.nvmrc` file. In other words, running `nvm use` et. al. in any subdirectory of a directory with an `.nvmrc` will result in that `.nvmrc` being utilized.
@@ -853,12 +862,19 @@ Put this into your `$HOME/.zshrc` to call `nvm use` automatically whenever you e
autoload -U add-zsh-hook
load-nvmrc(){
# skip during tab completion, which can run `cd` in a subshell
(( $+compstate ))&&return
local nvmrc_path
nvmrc_path="$(nvm_find_nvmrc)"
if[ -n "$nvmrc_path"];then
local nvmrc_version
# Let nvm parse `.nvmrc`, so that comments are ignored and the content of an invalid
# or symlinked `.nvmrc` is not passed along to other commands
To make development and testing work easier we supply a Dockerfile for development usage. It's based on an Ubuntu base image prepared with essential and useful tools for `nvm` development. To build the docker image of the environment, do a Docker build at the root of `nvm` repository:
```sh
$ docker build -t nvm-dev .
docker build -t nvm-dev .# Create the nvm-dev image
docker images # Confirm nvm-dev appears in the list.
# Create a container named `nvm-dev` (with the same hostname)
# and enter an interactive session in it.
docker run --name nvm-dev -h nvm-dev -it nvm-dev
```
This will package your current nvm working copy with our pre-defined development environment into a Docker image named `nvm-dev`. After the build you should see it appear in the list of images:
This will place you in the container in the `~/.nvm/` directory as the `nvm` user. When you exit the container will not be deleted (so as to avoid losing any work in it); you can re-enter the container with `docker start -i nvm-dev` or delete it permanently with `docker container rm -f nvm-dev`. (For more information on using Docker, see the [website][docker-www] and [documentation][docker-docs].)
```sh
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nvm-dev latest 9ca4c57a97d8 7 days ago 650 MB
```
To start and enter a container based on this image:
```sh
$ docker run -h nvm-dev -it nvm-dev
nvm@nvm-dev:~/.nvm$
```
It takes several minutes to build the image and the image size is about 650MB, so it's not suitable for production usage.
For more information and documentation about Docker, please refer to its [official website][docker-www] and [documentation][docker-docs]:
Note that the build takes several minutes and well over half a gigabyte of disk space, so it's not suitable for production usage.
@@ -47,6 +47,14 @@ nvm alias work --lts=jod 22 >/dev/null 2>&1 || die 'nvm alias work --lts=jod 22
nvm alias other --lts=iron 22 >/dev/null 2>&1
[ "_$(nvm version other)" = '_N/A' ] || die "no installed v22 is iron; got >$(nvm version other)<"
# an LTS name matches every codename containing it, but not as a regex
nvm alias some --lts=o node >/dev/null 2>&1 || die 'nvm alias some --lts=o node failed'
[ "_$(nvm version some)" = '_v24.11.0' ] || die "some should be the latest installed version whose codename contains o; got >$(nvm version some)<"
nvm alias some --lts=ir node >/dev/null 2>&1 || die 'nvm alias some --lts=ir node failed'
[ "_$(nvm version some)" = '_v20.19.0' ] || die "some should be the latest installed version whose codename contains ir; got >$(nvm version some)<"
nvm alias some --lts=. node >/dev/null 2>&1 || die 'nvm alias some --lts=. node failed'
[ "_$(nvm version some)" = '_N/A' ] || die "no codename contains a literal .; got >$(nvm version some)<"
OUTPUT="$(nvm unalias work)"
[ "_${OUTPUT}" = '_Deleted alias work - restore it with `nvm alias "work" --lts=jod "22"`' ] || die "the restore hint should keep --lts=jod as its own argument; got >${OUTPUT}<"
[ "$(command cat "${TEST_TARBALL}")" = 'downloaded' ] || die "verified download: expected the cached archive to hold only the new download, got >$(command cat "${TEST_TARBALL}")<"
[ ! -e "${TEST_TARBALL}.unverified" ] || die 'verified download: expected no unverified download to remain in the cache'
[ "$(command ls "${TEST_CACHE_DIR}")" = "$(nvm_echo 'files'; nvm_echo "${TEST_SLUG}.tar.gz")" ] || die "verified download: expected no unverified download to remain in the cache, got >$(command ls "${TEST_CACHE_DIR}")<"
[ ! -e "${WORK}/downloaded-to-final-name" ] || die 'an unverified download was written to the final cache name, where an interrupted download would be left for --offline'
# without any checksum tool, nothing is downloaded and the verified cache is left alone
nvm_get_checksum_binary() { return 1; }
try nvm_download_artifact node binary std v20.0.0
[ "${CAPTURED_EXIT_CODE}" = '6' ] || die "no checksum tool: expected exit code 6, got ${CAPTURED_EXIT_CODE}"
[ "$(command cat "${TEST_TARBALL}")" = 'downloaded' ] || die "no checksum tool: expected the cached archive to be left alone, got >$(command cat "${TEST_TARBALL}")<"
[ "$(command ls "${TEST_CACHE_DIR}")" = "$(nvm_echo 'files'; nvm_echo "${TEST_SLUG}.tar.gz")" ] || die "no checksum tool: expected nothing to be downloaded, got >$(command ls "${TEST_CACHE_DIR}")<"
# a failed `exec` redirection would end the shell; where the path is gone, curl would fail to open it
if [ -d "$(command dirname "$(command cat "${WORK}/first-download")")" ]; then
exec 9> "$(command cat "${WORK}/first-download")"
command touch "${WORK}/late-open"
fi
nvm_echo 'published' > "$2"
fi
}
try nvm_download_artifact node binary std v20.0.0
[ "$(command cat "${WORK}/second-install")" = "$(nvm_echo "${TEST_TARBALL}"; nvm_echo 0)" ] || die "late write: expected the second install to cache its download, got >$(command cat "${WORK}/second-install")<"
[ "$(command cat "${WORK}/cached-after-late-write")" = 'published' ] || die "late write: expected the verified archive in the cache to stay as it was, got >$(command cat "${WORK}/cached-after-late-write")<"
[ "${CAPTURED_EXIT_CODE}" = '6' ] || die "late write: expected the first install to reject its own download (exit code 6), got ${CAPTURED_EXIT_CODE}"
*"${ESC}"* | *"${BEL}"*) die "nvm_ls_remote printed control characters from the mirror's LTS codename: $(printf '%s' "${OUTPUT}" | od -c | head -5)" ;;
esac
case "${OUTPUT}" in
*'v21.0.0'* | *'v20.1.0'*) die "nvm_ls_remote listed a row with a malformed LTS codename: ${OUTPUT}" ;;
*'v20.0.0'*'Iron'*'v23.0.0'*) ;;
*) die "nvm_ls_remote did not list the valid rows: ${OUTPUT}" ;;
esac
if [ -f "$NVM_DIR/.lts-status" ] && nvm_grep -q "${ESC}" "$NVM_DIR/.lts-status"; then
die 'control characters from the mirror were recorded in .lts-status'
fi
# a checksum from the mirror is printed when it does not match; its control characters must not be
printf 'tarball' > "$WORK/tarball"
OUTPUT="$(nvm_compare_checksum "$WORK/tarball" "${ESC}]0;PWNED${BEL}${ESC}[31m" 2>&1)" && die 'nvm_compare_checksum accepted a mismatched checksum'
case "${OUTPUT}" in
*"${ESC}"* | *"${BEL}"*) die "nvm_compare_checksum printed control characters from the mirror's checksum: $(printf '%s' "${OUTPUT}" | od -c | head -5)" ;;
*"Checksums do not match"*"?]0;PWNED??[31m' expected."*) ;;
*) die "unexpected nvm_compare_checksum output: ${OUTPUT}" ;;
esac
cleanup
echo "nvm_ls_remote LTS codename control characters: passed"
[ "_$CODENAMES" = "_Argon Boron Carbon Iron " ] || die "\`NVM_LTS=on nvm_ls_remote\` should list the Argon, Boron, Carbon and Iron lines; got >$CODENAMES<"
for NVM_LTS in iron ir; do
OUTPUT="$(nvm_ls_remote)"
[ -n "$OUTPUT" ] || die "\`NVM_LTS=${NVM_LTS} nvm_ls_remote\` output nothing"
&& die "expected a symlink to ${TARGET} to be invalid"
[ "${CAPTURED_STDERR}" = "invalid .nvmrc! '${NVMRC}' is a symbolic link, so it must contain a version or an alias name; its content is not shown." ] || die "expected the symlink error for ${TARGET}; got \`${CAPTURED_STDERR}\`"
[ -s "${TEST_DIR}/arguments" ] || die 'expected the mocked commands to record their arguments'
! nvm_grep -q SECRET "${TEST_DIR}/arguments" || die "expected a symlink to ${TARGET} to not put its content in a command's arguments; got \`$(nvm_grep SECRET "${TEST_DIR}/arguments")\`"
ln -s ../home/accepted "${NVMRC}" || die 'failed to create the symlink'
: > "${TEST_DIR}/arguments"
CAPTURED_STDOUT="$(set +x; PATH="${MOCK_BIN}:${PATH}" nvm_process_nvmrc "${NVMRC}" 2>/dev/null)" || die 'expected a symlink to a valid version file to be valid'
[ "${CAPTURED_STDOUT}" = '20' ] || die "expected \`20\` from a symlink to a valid version file; got \`${CAPTURED_STDOUT}\`"
! nvm_grep -q SECRET "${TEST_DIR}/arguments" || die "expected a symlink to a valid version file to not put its content in a command's arguments; got \`$(nvm_grep SECRET "${TEST_DIR}/arguments")\`"
nvm alias nvmrc_symlink_alias v0.0.1 > /dev/null 2>&1 || die 'failed to create an alias'
# a deliberate symlink to another version file, in or above the project, still works
[ -e "${SWAP_DONE}" ] || die "$1: expected the mocked \`cat\` to swap the file"
case "${CAPTURED_STDOUT}${CAPTURED_STDERR}" in
*SECRET*) die "$1: expected the content to not be shown; got \`${CAPTURED_STDOUT}${CAPTURED_STDERR}\`" ;;
esac
! nvm_grep -q SECRET "${TEST_DIR}/arguments" || die "$1: expected the content to not be in a command's arguments; got \`$(nvm_grep SECRET "${TEST_DIR}/arguments")\`"
}
# a regular file, swapped for a symlink before it is read
rm -f "${NVMRC}"
printf 'v20.0.0\n' > "${NVMRC}"
SWAP_FROM="${TEST_DIR}/project/link"
ln -s ../home/.git-credentials "${SWAP_FROM}" || die 'failed to create the symlink'
check 'a regular file swapped for a symlink'
[ "${CAPTURED_EXIT_CODE}" = 0 ] || die "expected the regular file's version; got exit code ${CAPTURED_EXIT_CODE}"
[ "${CAPTURED_STDOUT}" = v20.0.0 ] || die "expected the regular file's version; got \`${CAPTURED_STDOUT}\`"
# a symlink, swapped for a regular file before it is checked
rm -f "${NVMRC}"
ln -s ../home/.git-credentials "${NVMRC}" || die 'failed to create the symlink'
SWAP_FROM="${TEST_DIR}/project/regular"
printf 'v20.0.0\n' > "${SWAP_FROM}"
check 'a symlink swapped for a regular file'
[ "${CAPTURED_EXIT_CODE}" != 0 ] || die "expected a symlink swapped for a regular file to be invalid; got exit code ${CAPTURED_EXIT_CODE}"
# a hard link to another file is treated like a symlink
rm -f "${NVMRC}"
ln "${TEST_DIR}/home/.git-credentials" "${NVMRC}" || die 'failed to create the hard link'
try_err nvm_process_nvmrc "${NVMRC}"
[ "${CAPTURED_EXIT_CODE}" != 0 ] || die "expected a hard link to be invalid; got exit code ${CAPTURED_EXIT_CODE}"
case "${CAPTURED_STDERR}" in
*SECRET*) die "expected a hard link's content to not be shown; got \`${CAPTURED_STDERR}\`" ;;
*) die "\"nvm_version node\" with a system node must list the installed versions only once; got \"nvm_ls $1\"" ;;
esac
}
try nvm_version node
[ "_$CAPTURED_STDOUT" = "_v22.1.0" ] || die '"nvm_version node" with a system node did not skip it; got '"$CAPTURED_STDOUT"
# with only 0.x versions installed, `stable` is the newest even minor, so it falls back to `nvm_ls stable`
nvm_ls() {
case "$1" in
node) printf 'v0.10.48\nv0.11.16\n' ;;
*) echo "pattern: $1" ;;
esac
}
try nvm_version node
[ "_$CAPTURED_STDOUT" = "_pattern: stable" ] || die '"nvm_version node" with only 0.x versions did not fall back to "nvm_ls stable"; got '"$CAPTURED_STDOUT"
# nothing installed falls back too, so the result and exit code are unchanged
nvm_ls() {
case "$1" in
node) echo 'N/A'; return 3 ;;
*) echo 'N/A'; return 3 ;;
esac
}
try nvm_version node
[ "_$CAPTURED_STDOUT" = "_N/A" ] || die '"nvm_version node" with nothing installed did not return N/A; got '"$CAPTURED_STDOUT"
[ "_$CAPTURED_EXIT_CODE" = "_3" ] || die '"nvm_version node" with nothing installed did not exit 3; got '"$CAPTURED_EXIT_CODE"
# an alias named `stable` takes precedence over the newest installed version
nvm_ls() {
case "$1" in
node) printf 'v20.0.0\nv22.1.0\n' ;;
*) echo "pattern: $1" ;;
esac
}
OLD_NVM_DIR="${NVM_DIR}"
NVM_DIR="$(mktemp -d)"
mkdir -p "${NVM_DIR}/alias"
echo v20.0.0 > "${NVM_DIR}/alias/stable"
try nvm_version node
rm -rf "${NVM_DIR}"
NVM_DIR="${OLD_NVM_DIR}"
[ "_$CAPTURED_STDOUT" = "_pattern: stable" ] || die '"nvm_version node" did not let an alias named "stable" take precedence; got '"$CAPTURED_STDOUT"
[ "$(cat "${TEST_DIR}/target")" = 'KEEPME' ] || die "expected a symlink swapped in after the check to not be written through; target is now \`$(cat "${TEST_DIR}/target")\`"
# 2. nor does a symlink to a directory get the file moved into it
) || die 'expected writing a new .nvmrc to succeed'
MODE="$(ls -l .nvmrc | cut -c1-10)"
[ "${MODE}" = '-rw-r-----' ] || die "expected .nvmrc to have mode -rw-r----- under umask 027; got ${MODE}"
# 5. a directory named .nvmrc is an error, and nothing is moved into it
rm -f .nvmrc
mkdir .nvmrc
try_err nvm_write_nvmrc "${TEST_VERSION}"
[ "${CAPTURED_EXIT_CODE}" = 3 ] || die "expected exit code 3 for a directory .nvmrc; got ${CAPTURED_EXIT_CODE}"
[ "${CAPTURED_STDERR}" = "Warning: Unable to write version number (${TEST_VERSION}) to .nvmrc" ] || die "expected the unable-to-write warning; got \`${CAPTURED_STDERR}\`"
[ -z "$(ls -A .nvmrc)" ] || die "expected nothing to be moved into a directory .nvmrc; found \`$(ls -A .nvmrc)\`"
rmdir .nvmrc
# 6. no temporary directories are left behind
LEFTOVER="$(ls -A "${NVM_DIR}" | grep '^\.nvmrc\.')"
[ -z "${LEFTOVER}" ] || die "expected no temporary directories left in \$NVM_DIR; found \`${LEFTOVER}\`"
[ -z "$message" ] || die "nvm_check_global_modules should have not printed a notice when npm had only itself installed as a global module; got:\n${message}"
# Faking the absence of npm
PATH=".:$PATH"
touch npm
chmod +x npm
FAKE_NPM_DIR="$(mktemp -d)"
touch "${FAKE_NPM_DIR}/npm"
chmod +x "${FAKE_NPM_DIR}/npm"
PATH="${FAKE_NPM_DIR}:$PATH"
message=$(nvm_check_global_modules)
[ -z "$message" ] || die "nvm_check_global_modules should have not printed a notice when npm was unavailable; got:\n${message}"
@@ -83,5 +84,20 @@ nvm_is_wsl() { return 1; }
message=$(nvm_check_global_modules)
[ -n "$message" ] || die 'nvm_check_global_modules should still warn about a Windows npm outside WSL, where it is the system node'
# npm must not apply the npmrc of the project that `$PWD` is in
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.