如何解决M系列CPU的Mac电脑中,nvm 无法安装低版本node的问题

1,363 阅读3分钟

M系列CPU的Mac电脑中,使用 nvm 安装 低版本的 node 会报错,因为node15以前的版本不提供mac arm64架构的二进制预编译包,解决版本是安装时使用-s 选项强制从源码安装,例如 nvm install -s 14.17 , 但是源码安装也会报错,你可能会遇到以下问题:

一. CPlease use python3.9 or python3.8 or python3.7 or python3.6 or python3.5 or python2.7.


> nvm install -s 14.17.0

Clang v3.5+ detected! CC or CXX not specified, will use Clang as C/C++ compiler!

Local cache found: ${NVM_DIR}/.cache/src/node-v14.17.0/node-v14.17.0.tar.xz

Checksums match! Using existing downloaded archive ${NVM_DIR}/.cache/src/node-v14.17.0/node-v14.17.0.tar.xz

$>./configure --prefix=/Users/shitian/.nvm/versions/node/v14.17.0 <

Node.js configure: Found Python 3.12.5...

Please use python3.9 or python3.8 or python3.7 or python3.6 or python3.5 or python2.7.

/Users/shitian/.nvm/.cache/src/node-v14.17.0/files/./configure:31: DeprecationWarning: Use shutil.which instead of find_executable

python_cmd_path = find_executable(python_cmd)

nvm: install v14.17.0 failed!

这个问题是因为需要在系统中安装python3.9或以下版本

解决方法:

  1. 安装 Python 3.9 (使用 Homebrew):

brew install python@3.9

  1. 如果你原来安装过其他版本,或者安装完后运行python3命令提示错误

你需要将 python3.9的安装目录加入环境变量, 例如上方使用的 brew 安装的 python3.9 , 可以使用命令which python3.9找到程序路径,将其加入环境变量,例如:export PATH="/opt/homebrew/bin:$PATH",一般情况下/opt/homebrew/bin早被加入了环境变量了,如果是你以前安装过别的版本,你可以把可执行文件 /opt/homebrew/bin/python3先备份,然后再将 python3.9 的可执行文件链接到 /usr/local/bin/python3


sudo mv /usr/local/bin/python3 /usr/local/bin/python3.bak

sudo ln -s /opt/homebrew/bin/python3.9 /usr/local/bin/python3

二、ModuleNotFoundError: No module named 'distutils'...


> nvm install -s 14.17.0

Clang v3.5+ detected! CC or CXX not specified, will use Clang as C/C++ compiler!

Local cache found: ${NVM_DIR}/.cache/src/node-v14.17.0/node-v14.17.0.tar.xz

Checksums match! Using existing downloaded archive ${NVM_DIR}/.cache/src/node-v14.17.0/node-v14.17.0.tar.xz

$>./configure --prefix=/Users/shitian/.nvm/versions/node/v14.17.0 <

Traceback (most recent call last):

File "/Users/shitian/.nvm/.cache/src/node-v14.17.0/files/./configure", line 21, in <module>

from distutils.spawn import find_executable

ModuleNotFoundError: No module named 'distutils'

nvm: install v14.17.0 failed!

这个是缺少 distutils 模块导致的,该模块是 Python 标准库的一部分,用于构建和安装 Python 包,一般会自动安装,如果你没有,可以使用以下命令安装


python3 -m ensurepip --upgrade

python3 -m pip install --upgrade setuptools

三、integer value 31 is outside the valid range of values [0, 15] for this enumeration type ....


../deps/v8/src/base/bit-field.h:43:29: error: integer value 31 is outside the valid range of values [0, 15] for this enumeration type [-Wenum-constexpr-conversion]

static constexpr T kMax = static_cast<T>(kNumValues - 1);

^

In file included from ../deps/v8/src/compiler/backend/gap-resolver.cc:5:

In file included from ../deps/v8/src/compiler/backend/gap-resolver.h:8:

In file included from ../deps/v8/src/compiler/backend/instruction.h:14:

In file included from ../deps/v8/src/codegen/external-reference.h:9:

In file included from ../deps/v8/src/runtime/runtime.h:11:

../deps/v8/src/base/bit-field.h:43:29: error: integer value 31 is outside the valid range of values [0, 15] for this enumeration type [-Wenum-constexpr-conversion]

static constexpr T kMax = static_cast<T>(kNumValues - 1);

^

../deps/v8/src/base/bit-field.h:43:29: error: integer value 3 is outside the valid range of values [0, 1] for this enumeration type [-Wenum-constexpr-conversion]

../deps/v8/src/base/bit-field.h:43:29: error: integer value 31 is outside the valid range of values [0, 15] for this enumeration type [-Wenum-constexpr-conversion]

1 error generated.

make[1]: *** [/Users/shitian/.nvm/.cache/src/node-v14.17.0/files/out/Release/obj.target/v8_compiler/deps/v8/src/compiler/all-nodes.o] Error 1

make[1]: *** Waiting for unfinished jobs....

../deps/v8/src/base/bit-field.h:43:29: error: integer value 3 is outside the valid range of values [0, 1] for this enumeration type [-Wenum-constexpr-conversion]

../deps/v8/src/base/bit-field.h:43:29: error: integer value 3 is outside the valid range of values [0, 1] for this enumeration type [-Wenum-constexpr-conversion]

../deps/v8/src/base/bit-field.h:43:29: error: integer value 31 is outside the valid range of values [0, 15] for this enumeration type [-Wenum-constexpr-conversion]

../deps/v8/src/base/bit-field.h:43:29: error: integer value 31 is outside the valid range of values [0, 15] for this enumeration type [-Wenum-constexpr-conversion]

1 error generated.

3 errors generated.

make[1]: *** [/Users/shitian/.nvm/.cache/src/node-v14.17.0/files/out/Release/obj.target/v8_compiler/deps/v8/src/compiler/add-type-assertions-reducer.o] Error 1

make[1]: *** [/Users/shitian/.nvm/.cache/src/node-v14.17.0/files/out/Release/obj.target/v8_compiler/deps/v8/src/compiler/backend/frame-elider.o] Error 1

3 errors generated.

make[1]: *** [/Users/shitian/.nvm/.cache/src/node-v14.17.0/files/out/Release/obj.target/v8_compiler/deps/v8/src/compiler/backend/gap-resolver.o] Error 1

../deps/v8/src/base/bit-field.h:43:29: error: integer value 7 is outside the valid range of values [0, 3] for this enumeration type [-Wenum-constexpr-conversion]

../deps/v8/src/base/bit-field.h:43:29: error: integer value 7 is outside the valid range of values [0, 3] for this enumeration type [-Wenum-constexpr-conversion]

../deps/v8/src/base/bit-field.h:43:29: error: integer value 7 is outside the valid range of values [0, 3] for this enumeration type [-Wenum-constexpr-conversion]

2 errors generated.

make[1]: *** [/Users/shitian/.nvm/.cache/src/node-v14.17.0/files/out/Release/obj.target/v8_compiler/deps/v8/src/compiler/access-builder.o] Error 1

2 errors generated.

make[1]: *** [/Users/shitian/.nvm/.cache/src/node-v14.17.0/files/out/Release/obj.target/v8_compiler/deps/v8/src/compiler/access-info.o] Error 1

4 errors generated.

make[1]: *** [/Users/shitian/.nvm/.cache/src/node-v14.17.0/files/out/Release/obj.target/v8_compiler/deps/v8/src/compiler/backend/code-generator.o] Error 1

rm 7d9d660a4dc0a75877e8a27a109d71e27cdc4a38.intermediate ac23c7831300b3870a23938574be0a717576496a.intermediate 64df151a6b58d28f4cdb482feb0ca8561d570b9c.intermediate

make: *** [node] Error 2

nvm: install v14.17.0 failed!

解决方法

这个问题是因为编译器版本太高了,你可以先安装12版本,brew install llvm@12,然后设置一下环境变量即可解决


export CC=/opt/homebrew/opt/llvm@12/bin/clang

export CXX=/opt/homebrew/opt/llvm@12/bin/clang++

愉快的安装

解决这些问题后,就可以顺利使用 nvm install -s 14.17.0 or nvm install -s 12.17.0 从源码安装低版本的node了,安装过程需要编译 node,我的MacBook Air M3 编译一次大概是 15 分钟左右,耐心等待吧...

> nvm list
->     v12.17.0
       v14.17.0
       v16.20.2
       v18.17.1
       v18.19.0
       v20.14.0