有需求,需要维护一个相对老的 Vue 项目,拿到项目之后,自然是先安装一下依赖,执行了 npm install 之后,看到了如下报错信息:

# 报错一

> node-sass@4.14.1 install /Users/eryajf/code/admin-front/node_modules/node-sass
> node scripts/install.js

Downloading binary from https://github.com/sass/node-sass/releases/download/v4.14.1/darwin-arm64-83_binding.node
Cannot download "https://github.com/sass/node-sass/releases/download/v4.14.1/darwin-arm64-83_binding.node":

HTTP error 404 Not Found

...... 有裁剪

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! node-sass@4.14.1 postinstall: `node scripts/build.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the node-sass@4.14.1 postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

# 报错二
gyp verb check python checking for Python executable "python2" in the PATH
gyp verb `which` failed Error: not found: python2
gyp verb `which` failed     at getNotFoundError (/Users/eryajf/code/admin-front/node_modules/which/which.js:13:12)
gyp verb `which` failed     at F (/Users/eryajf/code/admin-front/node_modules/which/which.js:68:19)
gyp verb `which` failed     at E (/Users/eryajf/code/admin-front/node_modules/which/which.js:80:29)
gyp verb `which` failed     at /Users/eryajf/code/admin-front/node_modules/which/which.js:89:16
gyp verb `which` failed     at /Users/eryajf/code/admin-front/node_modules/isexe/index.js:42:5
gyp verb `which` failed     at /Users/eryajf/code/admin-front/node_modules/isexe/mode.js:8:5
gyp verb `which` failed     at FSReqCallback.oncomplete (fs.js:192:21)
gyp verb `which` failed  python2 Error: not found: python2
gyp verb `which` failed     at getNotFoundError (/Users/eryajf/code/admin-front/node_modules/which/which.js:13:12)
gyp verb `which` failed     at F (/Users/eryajf/code/admin-front/node_modules/which/which.js:68:19)
gyp verb `which` failed     at E (/Users/eryajf/code/admin-front/node_modules/which/which.js:80:29)
gyp verb `which` failed     at /Users/eryajf/code/admin-front/node_modules/which/which.js:89:16
gyp verb `which` failed     at /Users/eryajf/code/admin-front/node_modules/isexe/index.js:42:5
gyp verb `which` failed     at /Users/eryajf/code/admin-front/node_modules/isexe/mode.js:8:5
gyp verb `which` failed     at FSReqCallback.oncomplete (fs.js:192:21) {
gyp verb `which` failed   code: 'ENOENT'
gyp verb `which` failed }
gyp verb check python checking for Python executable "python" in the PATH
gyp verb `which` succeeded python /usr/local/bin/python
gyp ERR! configure error
gyp ERR! stack Error: Command failed: /usr/local/bin/python -c import sys; print "%s.%s.%s" % sys.version_info[:3];
gyp ERR! stack   File "<string>", line 1
gyp ERR! stack     import sys; print "%s.%s.%s" % sys.version_info[:3];
gyp ERR! stack                       ^
gyp ERR! stack SyntaxError: invalid syntax
gyp ERR! stack
gyp ERR! stack     at ChildProcess.exithandler (child_process.js:383:12)
gyp ERR! stack     at ChildProcess.emit (events.js:400:28)
gyp ERR! stack     at maybeClose (internal/child_process.js:1088:16)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:296:5)
gyp ERR! System Darwin 21.6.0

如上报错分两个点,一个是 python 命令依赖 python2,一个是 node-saas 下载路径为 404。

网上搜索的解决方案一大堆,不过大多数都是没有效果的。

这里记录两种解决方案,针对于这个问题。

#方案一

我用的电脑是 Mac 的 M1 版本,系统里边默认的 Python 是 v3 的版本,所以如上错误是一个随着时间推移而产生的一个问题。

可以通过如下方式安装 Python 的 v2 来解决这个问题:

brew install pyenv
pyenv install 2.7.18
pyenv global 2.7.18

将环境变量放入系统配置文件(根据自己电脑的环境随时变化):

$ cat ~/.zshrc | grep pyenv
export PATH="$HOME/.pyenv/shims:$PATH"

然后执行命令验证一下环境:

$ python
Python 2.7.18 (default, Jan  1 2024, 10:21:04)
[GCC Apple LLVM 14.0.0 (clang-1400.0.29.201)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

看到版本为 v2 之后,再次回到项目中,执行 npm install,发现就能成功安装依赖了。

有一个小的注意点是,你需要确保 npm 安装时默认使用的是 python,而非其他路径,可通过 npm config list 查看,当然,如果你的 python 需要指向其他的,那么单独安装依赖的时候,可以通过如下命令指定当次的 python:

npm install --python=python2.7

也可通过如下命令设置默认的 python 版本:

npm config set python python

参考: issue(opens new window)

#方案二

从 node-sass 的角度来解决这个问题,在 node-sass 的 release 中可以看到,其并没有构建 arm 架构的二进制包,所以总是会构建失败。

但有一个方案是可以通过 sass 来替代 node-sass。

执行如下命令:

#卸载掉node-sass
npm uninstall node-sass
#安装sass
npm install sass

然后再次执行 npm install 就能成功安装依赖了。

只不过这种方案,会改变 package.json,目前还不知道会不会带来其他未可知的影响。

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐