Compare commits

...

11 Commits

Author SHA1 Message Date
Chris Hall
9982884b93
Merge a99b8b0ca33f7d8a988cd3647c4e22dd86562025 into b77fcec399d3c21645116263ce6212c137c13097 2025-02-03 20:14:59 +00:00
Björn Holm
b77fcec399
[readme] add docker tips
- covers installation using BASH_ENV

Co-authored-by: Christopher Dieringer <cdaringe@users.noreply.github.com>
Co-authored-by: Björn Holm <blollle@gmail.com>
2022-11-14 00:16:13 +01:00
Andrew Chang-DeWitt
287d535f2c
[readme] remove avn from readme
`avn` is archived ([see repo](https://github.com/direnv/direnv)) and should probably no longer be recommended by nvm.

This also adds a solution using `direnv` and proposes a possible `.envrc` to use.
See [wbyoung/avn#109](https://github.com/wbyoung/avn/issues/109#issuecomment-1281865585) for more suggested `.envrc` files.
2024-11-13 14:41:39 -06:00
Kid
572c757f6d
[readme] fnm -> nvm.fish 2021-07-18 18:32:40 +08:00
Jordan Harband
1d39e35bd3
[Tests] temporarily skip this failing travis test to unblock progress 2025-02-03 10:50:19 -08:00
Jordan Harband
19f452ba0f
[Fix] avoid bash-specific syntax
Fixes #3499
2024-12-19 22:21:09 -08:00
Jordan Harband
b9b0ea8020
[Fix] install-latest-npm: npm v11 is out 2024-12-16 10:03:18 -08:00
Jordan Harband
dae1427f7e
[actions] TOC: use latest LTS node 2024-11-22 21:44:36 -10:00
Matt Saladna
a99b8b0ca3
Add test for NVM_DIR outside nvm.sh 2018-06-21 16:38:15 -04:00
Matt Saladna
2cfe795599
shellcheck warnings 2018-06-21 14:38:55 -04:00
Matt Saladna
acbd22fc7e
Allow nvm-exec to be linked into individual .nvm directories for system-wide installs with a localized Nodes.
Let's say we have nvm installed in a separate mount, /.socket. NVM_DIR is $HOME/.nvm in /etc/profile.d/nvm.sh. With this setup, users can install Node versions to their home directories without each installing nvm.

nvm install --lts

This works fine as does nvm use --lts. When nvm exec is used though, it fails because it looks for nvm-exec in $NVM_DIR. First fix is to look for nvm-exec in $NVM_DIR. If NVM_DIR does not contain nvm-exec, check $BASH_SOURCE[0]. The second fix is to follow nvm-exec if a symbolic link to determine the proper location of nvm's home. Alternatively we could use a second environment variable, NVM_HOME in exec instead of relying on the directory name of nvm-exec.
2018-06-21 14:33:11 -04:00
8 changed files with 89 additions and 16 deletions

View File

@ -37,6 +37,11 @@ jobs:
matrix: matrix:
node-version: ${{ fromJson(needs.matrix.outputs.latest) }} node-version: ${{ fromJson(needs.matrix.outputs.latest) }}
include: include:
- node-version: "21"
- node-version: "20.5"
- node-version: "20.4"
- node-version: "14.17"
- node-version: "14.16"
- node-version: "9.2" - node-version: "9.2"
- node-version: "9.1" - node-version: "9.1"
- node-version: "9.0" - node-version: "9.0"

View File

@ -20,6 +20,7 @@ jobs:
allowed-endpoints: allowed-endpoints:
github.com:443 github.com:443
registry.npmjs.org:443 registry.npmjs.org:443
api.github.com:443
- uses: actions/checkout@v4 - uses: actions/checkout@v4
with: with:
# https://github.com/actions/checkout/issues/217#issue-599945005 # https://github.com/actions/checkout/issues/217#issue-599945005
@ -30,7 +31,7 @@ jobs:
- run: git fetch --depth=1 origin +refs/tags/*:refs/tags/* - run: git fetch --depth=1 origin +refs/tags/*:refs/tags/*
- uses: actions/setup-node@v4 - uses: actions/setup-node@v4
with: with:
node-version: '16' node-version: 'lts/*'
- run: npm install - run: npm install
- run: npm run doctoc - run: npm run doctoc
- name: commit changes - name: commit changes

View File

@ -91,16 +91,20 @@ RUN echo 'nvm ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers
# Switch to user "nvm" from now # Switch to user "nvm" from now
USER nvm USER nvm
# Create a script file sourced by both interactive and non-interactive bash shells
ENV BASH_ENV /home/nvm/.bash_env
RUN touch "$BASH_ENV"
RUN echo '. "$BASH_ENV"' >> "$HOME/.bashrc"
# nvm # nvm
RUN echo 'export NVM_DIR="$HOME/.nvm"' >> "$HOME/.bashrc" RUN echo 'export NVM_DIR="$HOME/.nvm"' >> "$BASH_ENV"
RUN echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm' >> "$HOME/.bashrc" RUN echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm' >> "$BASH_ENV"
RUN echo '[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion" # This loads nvm bash_completion' >> "$HOME/.bashrc" RUN echo '[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion" # This loads nvm bash_completion' >> "$BASH_ENV"
# nodejs and tools # nodejs and tools
RUN bash -c 'source $HOME/.nvm/nvm.sh && \ RUN nvm install node
nvm install node && \ RUN npm install -g doctoc urchin eclint dockerfile_lint
npm install -g doctoc urchin eclint dockerfile_lint && \ RUN npm install --prefix "$HOME/.nvm/"
npm install --prefix "$HOME/.nvm/"'
# Set WORKDIR to nvm directory # Set WORKDIR to nvm directory
WORKDIR /home/nvm/.nvm WORKDIR /home/nvm/.nvm

View File

@ -18,6 +18,7 @@
- [Installing and Updating](#installing-and-updating) - [Installing and Updating](#installing-and-updating)
- [Install & Update Script](#install--update-script) - [Install & Update Script](#install--update-script)
- [Additional Notes](#additional-notes) - [Additional Notes](#additional-notes)
- [Installing in Docker](#installing-in-docker)
- [Troubleshooting on Linux](#troubleshooting-on-linux) - [Troubleshooting on Linux](#troubleshooting-on-linux)
- [Troubleshooting on macOS](#troubleshooting-on-macos) - [Troubleshooting on macOS](#troubleshooting-on-macos)
- [Ansible](#ansible) - [Ansible](#ansible)
@ -129,6 +130,25 @@ Eg: `curl ... | NVM_DIR="path/to/nvm"`. Ensure that the `NVM_DIR` does not conta
- You can instruct the installer to not edit your shell config (for example if you already get completions via a [zsh nvm plugin](https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/nvm)) by setting `PROFILE=/dev/null` before running the `install.sh` script. Here's an example one-line command to do that: `PROFILE=/dev/null bash -c 'curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash'` - You can instruct the installer to not edit your shell config (for example if you already get completions via a [zsh nvm plugin](https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/nvm)) by setting `PROFILE=/dev/null` before running the `install.sh` script. Here's an example one-line command to do that: `PROFILE=/dev/null bash -c 'curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash'`
#### Installing in Docker
When invoking bash as a non-interactive shell, like in a Docker container, none of the regular profile files are sourced. In order to use `nvm`, `node`, and `npm` like normal, you can instead specify the special `BASH_ENV` variable, which bash sources when invoked non-interactively.
```Dockerfile
# Use bash for the shell
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
# Create a script file sourced by both interactive and non-interactive bash shells
ENV BASH_ENV /home/user/.bash_env
RUN touch "${BASH_ENV}"
RUN echo '. "${BASH_ENV}"' >> ~/.bashrc
# Download and install nvm
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | PROFILE="${BASH_ENV}" bash
RUN echo node > .nvmrc
RUN nvm install
```
#### Troubleshooting on Linux #### Troubleshooting on Linux
On Linux, after running the install script, if you get `nvm: command not found` or see no feedback from your terminal after you type `command -v nvm`, simply close your current terminal, open a new terminal, and try verifying again. On Linux, after running the install script, if you get `nvm: command not found` or see no feedback from your terminal after you type `command -v nvm`, simply close your current terminal, open a new terminal, and try verifying again.
@ -207,7 +227,7 @@ If you're running a system without prepackaged binary available, which means you
- [bass](https://github.com/edc/bass) allows you to use utilities written for Bash in fish shell - [bass](https://github.com/edc/bass) allows you to use utilities written for Bash in fish shell
- [fast-nvm-fish](https://github.com/brigand/fast-nvm-fish) only works with version numbers (not aliases) but doesn't significantly slow your shell startup - [fast-nvm-fish](https://github.com/brigand/fast-nvm-fish) only works with version numbers (not aliases) but doesn't significantly slow your shell startup
- [plugin-nvm](https://github.com/derekstavis/plugin-nvm) plugin for [Oh My Fish](https://github.com/oh-my-fish/oh-my-fish), which makes nvm and its completions available in fish shell - [plugin-nvm](https://github.com/derekstavis/plugin-nvm) plugin for [Oh My Fish](https://github.com/oh-my-fish/oh-my-fish), which makes nvm and its completions available in fish shell
- [fnm](https://github.com/fisherman/fnm) - [fisherman](https://github.com/fisherman/fisherman)-based version manager for fish - [nvm.fish](https://github.com/jorgebucaran/nvm.fish) - The Node.js version manager you'll adore, crafted just for Fish
- [fish-nvm](https://github.com/FabioAntunes/fish-nvm) - Wrapper around nvm for fish, delays sourcing nvm until it's actually used. - [fish-nvm](https://github.com/FabioAntunes/fish-nvm) - Wrapper around nvm for fish, delays sourcing nvm until it's actually used.
**Note:** We still have some problems with FreeBSD, because there is no official pre-built binary for FreeBSD, and building from source may need [patches](https://www.freshports.org/www/node/files/patch-deps_v8_src_base_platform_platform-posix.cc); see the issue ticket: **Note:** We still have some problems with FreeBSD, because there is no official pre-built binary for FreeBSD, and building from source may need [patches](https://www.freshports.org/www/node/files/patch-deps_v8_src_base_platform_platform-posix.cc); see the issue ticket:
@ -588,9 +608,7 @@ Run [`npx nvmrc`](https://npmjs.com/nvmrc) to validate an `.nvmrc` file. If that
### Deeper Shell Integration ### Deeper Shell Integration
You can use [`avn`](https://github.com/wbyoung/avn) to deeply integrate into your shell and automatically invoke `nvm` when changing directories. `avn` is **not** supported by the `nvm` maintainers. Please [report issues to the `avn` team](https://github.com/wbyoung/avn/issues/new). You can use [`nvshim`](https://github.com/iamogbz/nvshim) to shim the `node`, `npm`, and `npx` bins to automatically use the `nvm` config in the current directory. `nvshim` is **not** supported by the `nvm` maintainers. Please [report issues to the `nvshim` team](https://github.com/iamogbz/nvshim/issues/new).
You can also use [`nvshim`](https://github.com/iamogbz/nvshim) to shim the `node`, `npm`, and `npx` bins to automatically use the `nvm` config in the current directory. `nvshim` is **not** supported by the `nvm` maintainers. Please [report issues to the `nvshim` team](https://github.com/iamogbz/nvshim/issues/new).
If you prefer a lighter-weight solution, the recipes below have been contributed by `nvm` users. They are **not** supported by the `nvm` maintainers. We are, however, accepting pull requests for more examples. If you prefer a lighter-weight solution, the recipes below have been contributed by `nvm` users. They are **not** supported by the `nvm` maintainers. We are, however, accepting pull requests for more examples.

View File

@ -1,6 +1,6 @@
#!/usr/bin/env bash #!/usr/bin/env bash
DIR="$(command cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
unset NVM_CD_FLAGS unset NVM_CD_FLAGS

33
nvm.sh
View File

@ -354,6 +354,21 @@ nvm_install_latest_npm() {
if [ $NVM_IS_19_OR_ABOVE -eq 1 ] && nvm_version_greater_than_or_equal_to "${NODE_VERSION}" 20.5.0; then if [ $NVM_IS_19_OR_ABOVE -eq 1 ] && nvm_version_greater_than_or_equal_to "${NODE_VERSION}" 20.5.0; then
NVM_IS_20_5_OR_ABOVE=1 NVM_IS_20_5_OR_ABOVE=1
fi fi
local NVM_IS_20_17_or_ABOVE
NVM_IS_20_17_or_ABOVE=0
if [ $NVM_IS_20_5_OR_ABOVE -eq 1 ] && nvm_version_greater 20.17.0 "${NODE_VERSION}"; then
NVM_IS_20_17_or_ABOVE=1
fi
local NVM_IS_21_OR_ABOVE
NVM_IS_21_OR_ABOVE=0
if [ $NVM_IS_20_17_or_ABOVE -eq 1 ] && nvm_version_greater 21.0.0 "${NODE_VERSION}"; then
NVM_IS_21_OR_ABOVE=1
fi
local NVM_IS_22_9_OR_ABOVE
NVM_IS_22_9_OR_ABOVE=0
if [ $NVM_IS_21_OR_ABOVE -eq 1 ] && nvm_version_greater 22.9.0 "${NODE_VERSION}"; then
NVM_IS_22_9_OR_ABOVE=1
fi
if [ $NVM_IS_4_4_OR_BELOW -eq 1 ] || { if [ $NVM_IS_4_4_OR_BELOW -eq 1 ] || {
[ $NVM_IS_5_OR_ABOVE -eq 1 ] && nvm_version_greater 5.10.0 "${NODE_VERSION}"; \ [ $NVM_IS_5_OR_ABOVE -eq 1 ] && nvm_version_greater 5.10.0 "${NODE_VERSION}"; \
@ -399,8 +414,15 @@ nvm_install_latest_npm() {
[ $NVM_IS_18_17_OR_ABOVE -eq 0 ] \ [ $NVM_IS_18_17_OR_ABOVE -eq 0 ] \
|| { [ $NVM_IS_19_OR_ABOVE -eq 1 ] && [ $NVM_IS_20_5_OR_ABOVE -eq 0 ]; } \ || { [ $NVM_IS_19_OR_ABOVE -eq 1 ] && [ $NVM_IS_20_5_OR_ABOVE -eq 0 ]; } \
; then ; then
# TODO: 10.8.3 can run on 16.20.2?? https://github.com/nodejs/Release/issues/884#issuecomment-2558077691
nvm_echo '* `npm` `v9.x` is the last version that works on `node` `< v18.17`, `v19`, or `v20.0` - `v20.4`' nvm_echo '* `npm` `v9.x` is the last version that works on `node` `< v18.17`, `v19`, or `v20.0` - `v20.4`'
$NVM_NPM_CMD install -g npm@9 $NVM_NPM_CMD install -g npm@9
elif \
[ $NVM_IS_20_17_or_ABOVE -eq 0 ] \
|| { [ $NVM_IS_21_OR_ABOVE -eq 1 ] && [ $NVM_IS_22_9_OR_ABOVE -eq 0 ]; } \
; then
nvm_echo '* `npm` `v10.x` is the last version that works on `node` `< v20.17`, `v21`, or `v22.0` - `v22.8`'
$NVM_NPM_CMD install -g npm@10
else else
nvm_echo '* Installing latest `npm`; if this does not work on your node version, please report a bug!' nvm_echo '* Installing latest `npm`; if this does not work on your node version, please report a bug!'
$NVM_NPM_CMD install -g npm $NVM_NPM_CMD install -g npm
@ -422,8 +444,7 @@ fi
if [ -z "${NVM_DIR-}" ]; then if [ -z "${NVM_DIR-}" ]; then
# shellcheck disable=SC2128 # shellcheck disable=SC2128
if [ -n "${BASH_SOURCE-}" ]; then if [ -n "${BASH_SOURCE-}" ]; then
# shellcheck disable=SC2169,SC3054 NVM_SCRIPT_SOURCE="${BASH_SOURCE}"
NVM_SCRIPT_SOURCE="${BASH_SOURCE[0]}"
fi fi
# shellcheck disable=SC2086 # shellcheck disable=SC2086
NVM_DIR="$(nvm_cd ${NVM_CD_FLAGS} "$(dirname "${NVM_SCRIPT_SOURCE:-$0}")" >/dev/null && \pwd)" NVM_DIR="$(nvm_cd ${NVM_CD_FLAGS} "$(dirname "${NVM_SCRIPT_SOURCE:-$0}")" >/dev/null && \pwd)"
@ -4054,8 +4075,14 @@ nvm() {
nvm_echo "Running node ${VERSION}$(nvm use --silent "${VERSION}" && nvm_print_npm_version)" nvm_echo "Running node ${VERSION}$(nvm use --silent "${VERSION}" && nvm_print_npm_version)"
fi fi
fi fi
NODE_VERSION="${VERSION}" "${NVM_DIR}/nvm-exec" "$@"
NVM_EXEC="${NVM_DIR}/nvm-exec"
if [ ! -f "${NVM_EXEC}" ]; then
NVM_EXEC="$(dirname "${BASH_SOURCE[0]-}")/nvm-exec"
fi
NODE_VERSION="${VERSION}" "${NVM_EXEC}" "$@"
;; ;;
"ls" | "list") "ls" | "list")
local PATTERN local PATTERN
local NVM_NO_COLORS local NVM_NO_COLORS

View File

@ -0,0 +1,18 @@
#!/bin/sh
set -ex
die () { echo "$@" ; exit 1; }
INSTPATH="$(mktemp -p "$(pwd)" -d)"
trap 'test ! -z "${INSTPATH-}" && test -d "$INSTPATH" && rm -rf "$INSTPATH"' EXIT
declare -x NVM_DIR=$INSTPATH
\. ../../../nvm.sh
nvm install --lts || die 'nvm install --lts failed'
nvm exec --lts npm --version || die "`nvm exec` failed to run"
declare -x NODE_VERSION="$(nvm exec --lts --silent node --version)"
ln -s ../../../../nvm-exec "$INSTPATH/nvm-exec" || die "failed to create a symlink to $INSTPATH/"
"$INSTPATH/nvm-exec" npm ls > /dev/null || die "`nvm exec` failed to run using nvm-exec helper"