mirror of
https://github.com/nvm-sh/nvm.git
synced 2025-12-11 15:44:19 +00:00
Compare commits
7 Commits
v0.39.1
...
xcode-sele
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6cfaede5a0 | ||
|
|
1875fe8b40 | ||
|
|
81f0f3ec19 | ||
|
|
fb4538b360 | ||
|
|
d004c6b064 | ||
|
|
79ad72d116 | ||
|
|
ccd442d833 |
105
README.md
105
README.md
@@ -903,57 +903,70 @@ Homebrew causes insecure directories like `/usr/local/share/zsh/site-functions`
|
|||||||
|
|
||||||
**Macs with M1 chip**
|
**Macs with M1 chip**
|
||||||
|
|
||||||
_January 2021:_ there are no pre-compiled NodeJS binaries for versions prior to 15.x for Apple's new M1 chip (arm64 architecture).
|
Experimental support for the M1 architecture was added in node.js v15.3 and full support was added in v16.0.
|
||||||
|
Because of this, if you try to install older versions of node as usual, you will probably experience either compilation errors when installing node or out-of-memory errors while running your code.
|
||||||
|
|
||||||
Some issues you may encounter:
|
So, if you want to run a version prior to v16.0 on an M1 Mac, it may be best to compile node targeting the x86_64 Intel architecture so that Rosetta 2 can translate the x86_64 processor instructions to ARM-based Apple Silicon instructions.
|
||||||
|
Here's what you will need to do:
|
||||||
|
|
||||||
- using `nvm` to install, say, `v14.15.4`:
|
- Install Rosetta, if you haven't already done so
|
||||||
- the C code compiles successfully
|
|
||||||
- but crashes with an out of memory error when used
|
|
||||||
- increasing the memory available to node still produces the out of memory errors:
|
|
||||||
```sh
|
|
||||||
$ NODE_OPTIONS="--max-old-space-size=4096" ./node_modules/.bin/your_node_package
|
|
||||||
```
|
|
||||||
- when using `nvm` to install some versions, the compilation fails
|
|
||||||
- after `nvm` successfully compiles some versions, `yarn` or `npm` may later fail to install packages with an `incorrect data check` error.
|
|
||||||
|
|
||||||
One solution to this issue is to change the architecture of your shell from arm64 to x86.
|
```sh
|
||||||
|
$ softwareupdate --install-rosetta
|
||||||
|
```
|
||||||
|
|
||||||
Let's assume that:
|
You might wonder, "how will my M1 Mac know to use Rosetta for a version of node compiled for an Intel chip?".
|
||||||
- you already have versions `12.20.1` and `14.15.4` installed using `nvm`
|
If an executable contains only Intel instructions, macOS will automatically use Rosetta to translate the instructions.
|
||||||
- the current version in use is `14.15.4`
|
|
||||||
- you are using the `zsh` shell
|
|
||||||
- you have Rosetta 2 installed (macOS prompts you to install Rosetta 2 the first time you open a Intel-only non-command-line application, or you may install Rosetta 2 from the command line with `softwareupdate --install-rosetta`)
|
|
||||||
|
|
||||||
```sh
|
- Open a shell that's running using Rosetta
|
||||||
# Check what version you're running:
|
|
||||||
$ node --version
|
```sh
|
||||||
v14.15.4
|
$ arch -x86_64 zsh
|
||||||
# Check architecture of the `node` binary:
|
```
|
||||||
$ node -p process.arch
|
|
||||||
arm64
|
Note: This same thing can also be accomplished by finding the Terminal or iTerm App in Finder, right clicking, selecting "Get Info", and then checking the box labeled "Open using Rosetta".
|
||||||
# This confirms that the arch is for the M1 chip, which is causing the problems.
|
|
||||||
# So we need to uninstall it.
|
Note: This terminal session is now running in `zsh`.
|
||||||
# We can't uninstall the version we are currently using, so switch to another version:
|
If `zsh` is not the shell you typically use, `nvm` may not be `source`'d automatically like it probably is for your usual shell through your dotfiles.
|
||||||
$ nvm install v12.20.1
|
If that's the case, make sure to source `nvm`.
|
||||||
# Now uninstall the version we want to replace:
|
|
||||||
$ nvm uninstall v14.15.4
|
```sh
|
||||||
# Launch a new zsh process under the 64-bit X86 architecture:
|
$ source "${NVM_DIR}/.nvm/nvm.sh"
|
||||||
$ arch -x86_64 zsh
|
```
|
||||||
# Install node using nvm. This should download the precompiled x64 binary:
|
|
||||||
$ nvm install v14.15.4
|
- Install whatever older version of node you are interested in. Let's use 12.22.1 as an example.
|
||||||
# Now check that the architecture is correct:
|
This will fetch the node source code and compile it, which will take several minutes.
|
||||||
$ node -p process.arch
|
|
||||||
x64
|
```sh
|
||||||
# It is now safe to return to the arm64 zsh process:
|
$ nvm install v12.22.1 --shared-zlib
|
||||||
$ exit
|
```
|
||||||
# We're back to a native shell:
|
|
||||||
$ arch
|
Note: You're probably curious why `--shared-zlib` is included.
|
||||||
arm64
|
There's a bug in recent versions of Apple's system `clang` compiler.
|
||||||
# And the new version is now available to use:
|
If one of these broken versions is installed on your system, the above step will likely still succeed even if you didn't include the `--shared-zlib` flag.
|
||||||
$ nvm use v14.15.4
|
However, later, when you attempt to `npm install` something using your old version of node.js, you will see `incorrect data check` errors.
|
||||||
Now using node v14.15.4 (npm v6.14.10)
|
If you want to avoid the possible hassle of dealing with this, include that flag.
|
||||||
```
|
For more details, see [this issue](https://github.com/nodejs/node/issues/39313) and [this comment](https://github.com/nodejs/node/issues/39313#issuecomment-902395576)
|
||||||
|
|
||||||
|
- Exit back to your native shell.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
$ exit
|
||||||
|
$ arch
|
||||||
|
arm64
|
||||||
|
```
|
||||||
|
|
||||||
|
Note: If you selected the box labeled "Open using Rosetta" rather than running the CLI command in the second step, you will see `i386` here.
|
||||||
|
Unless you have another reason to have that box selected, you can deselect it now.
|
||||||
|
|
||||||
|
- Check to make sure the architecture is correct. `x64` is the abbreviation for x86_64, which is what you want to see.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
$ node -p process.arch
|
||||||
|
x64
|
||||||
|
```
|
||||||
|
|
||||||
|
Now you should be able to use node as usual.
|
||||||
|
|
||||||
## Maintainers
|
## Maintainers
|
||||||
|
|
||||||
|
|||||||
@@ -356,6 +356,12 @@ nvm_do_install() {
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
if nvm_has xcode-select && [ "$(xcode-select -p >/dev/null 2>/dev/null ; echo $?)" = '2' ] && [ "$(which git)" = '/usr/bin/git' ] && [ "$(which curl)" = '/usr/bin/curl' ]; then
|
||||||
|
nvm_echo >&2 'You may be on a Mac, and need to install the Xcode Command Line Developer Tools.'
|
||||||
|
# shellcheck disable=SC2016
|
||||||
|
nvm_echo >&2 'If so, run `xcode-select --install` and try again. If not, please report this!'
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
if [ -z "${METHOD}" ]; then
|
if [ -z "${METHOD}" ]; then
|
||||||
# Autodetect install method
|
# Autodetect install method
|
||||||
if nvm_has git; then
|
if nvm_has git; then
|
||||||
|
|||||||
90
nvm.sh
90
nvm.sh
@@ -1870,9 +1870,12 @@ nvm_get_arch() {
|
|||||||
*) NVM_ARCH="${HOST_ARCH}" ;;
|
*) NVM_ARCH="${HOST_ARCH}" ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
# If running a 64bit ARM kernel but a 32bit ARM userland, change ARCH to 32bit ARM (armv7l)
|
# If running a 64bit ARM kernel but a 32bit ARM userland,
|
||||||
L=$(ls -dl /sbin/init 2>/dev/null) # if /sbin/init is 32bit executable
|
# change ARCH to 32bit ARM (armv7l) if /sbin/init is 32bit executable
|
||||||
if [ "$(uname)" = "Linux" ] && [ "${NVM_ARCH}" = arm64 ] && [ "$(od -An -t x1 -j 4 -N 1 "${L#*-> }")" = ' 01' ]; then
|
local L
|
||||||
|
if [ "$(uname)" = "Linux" ] && [ "${NVM_ARCH}" = arm64 ] &&
|
||||||
|
L="$(ls -dl /sbin/init 2>/dev/null)" &&
|
||||||
|
[ "$(od -An -t x1 -j 4 -N 1 "${L#*-> }")" = ' 01' ]; then
|
||||||
NVM_ARCH=armv7l
|
NVM_ARCH=armv7l
|
||||||
HOST_ARCH=armv7l
|
HOST_ARCH=armv7l
|
||||||
fi
|
fi
|
||||||
@@ -1971,19 +1974,7 @@ nvm_install_binary_extract() {
|
|||||||
command unzip -q "${TARBALL}" -d "${TMPDIR}" || return 1
|
command unzip -q "${TARBALL}" -d "${TMPDIR}" || return 1
|
||||||
# For non Windows system (including WSL running on Windows)
|
# For non Windows system (including WSL running on Windows)
|
||||||
else
|
else
|
||||||
local tar_compression_flag
|
nvm_extract_tarball "${NVM_OS}" "${VERSION}" "${TARBALL}" "${TMPDIR}"
|
||||||
tar_compression_flag='z'
|
|
||||||
if nvm_supports_xz "${VERSION}"; then
|
|
||||||
tar_compression_flag='J'
|
|
||||||
fi
|
|
||||||
|
|
||||||
local tar
|
|
||||||
if [ "${NVM_OS}" = 'aix' ]; then
|
|
||||||
tar='gtar'
|
|
||||||
else
|
|
||||||
tar='tar'
|
|
||||||
fi
|
|
||||||
command "${tar}" -x${tar_compression_flag}f "${TARBALL}" -C "${TMPDIR}" --strip-components 1 || return 1
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
command mkdir -p "${VERSION_PATH}" || return 1
|
command mkdir -p "${VERSION_PATH}" || return 1
|
||||||
@@ -2251,6 +2242,48 @@ nvm_download_artifact() {
|
|||||||
nvm_echo "${TARBALL}"
|
nvm_echo "${TARBALL}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# args: nvm_os, version, tarball, tmpdir
|
||||||
|
nvm_extract_tarball() {
|
||||||
|
if [ "$#" -ne 4 ]; then
|
||||||
|
nvm_err 'nvm_extract_tarball requires exactly 4 arguments'
|
||||||
|
return 5
|
||||||
|
fi
|
||||||
|
|
||||||
|
local NVM_OS
|
||||||
|
NVM_OS="${1-}"
|
||||||
|
|
||||||
|
local VERSION
|
||||||
|
VERSION="${2-}"
|
||||||
|
|
||||||
|
local TARBALL
|
||||||
|
TARBALL="${3-}"
|
||||||
|
|
||||||
|
local TMPDIR
|
||||||
|
TMPDIR="${4-}"
|
||||||
|
|
||||||
|
local tar_compression_flag
|
||||||
|
tar_compression_flag='z'
|
||||||
|
if nvm_supports_xz "${VERSION}"; then
|
||||||
|
tar_compression_flag='J'
|
||||||
|
fi
|
||||||
|
|
||||||
|
local tar
|
||||||
|
tar='tar'
|
||||||
|
if [ "${NVM_OS}" = 'aix' ]; then
|
||||||
|
tar='gtar'
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "${NVM_OS}" = 'openbsd' ]; then
|
||||||
|
if [ "${tar_compression_flag}" = 'J' ]; then
|
||||||
|
command xzcat "${TARBALL}" | "${tar}" -xf - -C "${TMPDIR}" -s '/[^\/]*\///' || return 1
|
||||||
|
else
|
||||||
|
command "${tar}" -x${tar_compression_flag}f "${TARBALL}" -C "${TMPDIR}" -s '/[^\/]*\///' || return 1
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
command "${tar}" -x${tar_compression_flag}f "${TARBALL}" -C "${TMPDIR}" --strip-components 1 || return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
nvm_get_make_jobs() {
|
nvm_get_make_jobs() {
|
||||||
if nvm_is_natural_num "${1-}"; then
|
if nvm_is_natural_num "${1-}"; then
|
||||||
NVM_MAKE_JOBS="$1"
|
NVM_MAKE_JOBS="$1"
|
||||||
@@ -2362,18 +2395,6 @@ nvm_install_source() {
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local tar_compression_flag
|
|
||||||
tar_compression_flag='z'
|
|
||||||
if nvm_supports_xz "${VERSION}"; then
|
|
||||||
tar_compression_flag='J'
|
|
||||||
fi
|
|
||||||
|
|
||||||
local tar
|
|
||||||
tar='tar'
|
|
||||||
if [ "${NVM_OS}" = 'aix' ]; then
|
|
||||||
tar='gtar'
|
|
||||||
fi
|
|
||||||
|
|
||||||
local TARBALL
|
local TARBALL
|
||||||
local TMPDIR
|
local TMPDIR
|
||||||
local VERSION_PATH
|
local VERSION_PATH
|
||||||
@@ -2393,7 +2414,7 @@ nvm_install_source() {
|
|||||||
if ! (
|
if ! (
|
||||||
# shellcheck disable=SC2086
|
# shellcheck disable=SC2086
|
||||||
command mkdir -p "${TMPDIR}" && \
|
command mkdir -p "${TMPDIR}" && \
|
||||||
command "${tar}" -x${tar_compression_flag}f "${TARBALL}" -C "${TMPDIR}" --strip-components 1 && \
|
nvm_extract_tarball "${NVM_OS}" "${VERSION}" "${TARBALL}" "${TMPDIR}" && \
|
||||||
VERSION_PATH="$(nvm_version_path "${PREFIXED_VERSION}")" && \
|
VERSION_PATH="$(nvm_version_path "${PREFIXED_VERSION}")" && \
|
||||||
nvm_cd "${TMPDIR}" && \
|
nvm_cd "${TMPDIR}" && \
|
||||||
nvm_echo '$>'./configure --prefix="${VERSION_PATH}" $ADDITIONAL_PARAMETERS'<' && \
|
nvm_echo '$>'./configure --prefix="${VERSION_PATH}" $ADDITIONAL_PARAMETERS'<' && \
|
||||||
@@ -2723,6 +2744,15 @@ nvm() {
|
|||||||
EXIT_CODE="$?"
|
EXIT_CODE="$?"
|
||||||
set -a
|
set -a
|
||||||
return "$EXIT_CODE"
|
return "$EXIT_CODE"
|
||||||
|
elif [ -n "${BASH-}" ] && [ "${-#*E}" != "$-" ]; then
|
||||||
|
# shellcheck disable=SC3041
|
||||||
|
set +E
|
||||||
|
local EXIT_CODE
|
||||||
|
IFS="${DEFAULT_IFS}" nvm "$@"
|
||||||
|
EXIT_CODE="$?"
|
||||||
|
# shellcheck disable=SC3041
|
||||||
|
set -E
|
||||||
|
return "$EXIT_CODE"
|
||||||
elif [ "${IFS}" != "${DEFAULT_IFS}" ]; then
|
elif [ "${IFS}" != "${DEFAULT_IFS}" ]; then
|
||||||
IFS="${DEFAULT_IFS}" nvm "$@"
|
IFS="${DEFAULT_IFS}" nvm "$@"
|
||||||
return "$?"
|
return "$?"
|
||||||
@@ -4151,7 +4181,7 @@ nvm() {
|
|||||||
nvm_npmrc_bad_news_bears \
|
nvm_npmrc_bad_news_bears \
|
||||||
nvm_get_colors nvm_set_colors nvm_print_color_code nvm_format_help_message_colors \
|
nvm_get_colors nvm_set_colors nvm_print_color_code nvm_format_help_message_colors \
|
||||||
nvm_echo_with_colors nvm_err_with_colors \
|
nvm_echo_with_colors nvm_err_with_colors \
|
||||||
nvm_get_artifact_compression nvm_install_binary_extract \
|
nvm_get_artifact_compression nvm_install_binary_extract nvm_extract_tarball \
|
||||||
>/dev/null 2>&1
|
>/dev/null 2>&1
|
||||||
unset NVM_RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_IOJS_ORG_MIRROR NVM_DIR \
|
unset NVM_RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_IOJS_ORG_MIRROR NVM_DIR \
|
||||||
NVM_CD_FLAGS NVM_BIN NVM_INC NVM_MAKE_JOBS \
|
NVM_CD_FLAGS NVM_BIN NVM_INC NVM_MAKE_JOBS \
|
||||||
|
|||||||
12
test/fast/Unit tests/nvm_extract_tarball
Executable file
12
test/fast/Unit tests/nvm_extract_tarball
Executable file
@@ -0,0 +1,12 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
die () { echo "$@" ; exit 1; }
|
||||||
|
|
||||||
|
\. ../../../nvm.sh
|
||||||
|
|
||||||
|
[ "$(nvm_extract_tarball 2>&1)" = "nvm_extract_tarball requires exactly 4 arguments" ] || die 'incorrect error message with no args'
|
||||||
|
[ "$(nvm_extract_tarball > /dev/null 2>&1 ; echo $?)" = "5" ] || die 'incorrect error code with no args'
|
||||||
|
[ "$(nvm_extract_tarball one two three 2>&1)" = "nvm_extract_tarball requires exactly 4 arguments" ] || die 'incorrect error message with three args'
|
||||||
|
[ "$(nvm_extract_tarball one two three > /dev/null 2>&1 ; echo $?)" = "5" ] || die 'incorrect error code with three args'
|
||||||
|
[ "$(nvm_extract_tarball one two three four five 2>&1)" = "nvm_extract_tarball requires exactly 4 arguments" ] || die 'incorrect error message with five args'
|
||||||
|
[ "$(nvm_extract_tarball one two three four five > /dev/null 2>&1 ; echo $?)" = "5" ] || die 'incorrect error code with five args'
|
||||||
50
test/installation_node/install on bash with ERR trap and set -E
Executable file
50
test/installation_node/install on bash with ERR trap and set -E
Executable file
@@ -0,0 +1,50 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
cleanup() {
|
||||||
|
nvm cache clear
|
||||||
|
nvm deactivate
|
||||||
|
rm -rf "${NVM_DIR}"/v*
|
||||||
|
nvm unalias default
|
||||||
|
}
|
||||||
|
|
||||||
|
die() {
|
||||||
|
echo "$@"
|
||||||
|
cleanup || true
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
\. ../../nvm.sh
|
||||||
|
|
||||||
|
if [ -z "${BASH-}" ]; then
|
||||||
|
echo "This test only applies to Bash; skipping"
|
||||||
|
exit
|
||||||
|
fi
|
||||||
|
|
||||||
|
cleanup || true
|
||||||
|
trap 'echo "==> EXIT signal received (status: $?)"; cleanup' EXIT
|
||||||
|
|
||||||
|
# shellcheck disable=SC3047
|
||||||
|
trap 'echo "==> ERR signal received"; exit 1' ERR
|
||||||
|
# shellcheck disable=SC3041
|
||||||
|
set -E
|
||||||
|
|
||||||
|
# shellcheck disable=SC3045,SC3047
|
||||||
|
ERR_TRAP_EXPECTED="$(trap -p ERR)"
|
||||||
|
|
||||||
|
# Adding ` || die 'install failed'` implicitly disables error handling and
|
||||||
|
# prevents ERR trap execution, so for the purposes of this test, `nvm install`
|
||||||
|
# can't be part of another command or statement
|
||||||
|
nvm install node
|
||||||
|
|
||||||
|
case "$-" in
|
||||||
|
*E*)
|
||||||
|
# shellcheck disable=SC3045,SC3047
|
||||||
|
[ "$(trap -p ERR)" = "$ERR_TRAP_EXPECTED" ] ||
|
||||||
|
die "ERR trap not restored after \"nvm install $VERSION\""
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
die "errtrace not restored after \"nvm install $VERSION\""
|
||||||
|
;;
|
||||||
|
esac
|
||||||
Reference in New Issue
Block a user