【解决方案】mac:stack Error: `gyp` failed with exit code:1

【20200730更新】
最下面是之前的答案。我发现这个问题其实不那么简单,是因为搜索gypfailed with exit code:1还不够具体,看看报的错误,其实可以更详细一些的。分隔符下面的是之前的一种解决方案,这种解决方案只解决报文件node-gyp/lib/configure.js:305:16的错,如果是这个问题参考最下面的解决方案。如果不是,那仔细看自己的问题,有时候还会发现另一种问题就是如下的问题:

ERROR:root:code for hash md5 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
...
ValueError: unsupported hash type md5
ERROR:root:code for hash sha1 was not found.
...
ValueError: unsupported hash type sha224
ERROR:root:code for hash sha256 was not found
...
  File "/usr/local/lib/node_modules/npm/node_modules/node-gyp/gyp/pylib/gyp/xcodeproj_file.py", line 152, in <module>
    _new_sha1 = hashlib.sha1
AttributeError: 'module' object has no attribute 'sha1'
gyp ERR! configure error
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onCpExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:345:16)
gyp ERR! stack     at ChildProcess.emit (events.js:189:13)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12)
gyp ERR! System Darwin 19.5.0
gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /Users/eric/Desktop/admin-web/node_modules/watchpack-chokidar2/node_modules/fsevents
gyp ERR! node -v v10.15.3
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok

仔细看这个问题,主要是/usr/local/Cellar/python@2/2.7.15/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py这个文件出了错,而且出错的都是md5sha1加密之类的方法没找到。所以这个文件出的问题,应该是由于我们电脑环境引起的,电脑中肯定某个库除了问题,导致执行这个文件的时候和加密相关的库或方法都挂了。所以搜索的时候可以详细搜索mac ERROR:root:code for hash md5 was not found之类的,而且在bing.com国际版上搜索,这样StackOverflow上如果有相关问题可以直接看到。果不其然啊,有一个问题再这里,和我们遇到的情况是一样的,主要原因是我们电脑上的openssl出问题了。可能是由于我们之前安装了多个版本的python,又或者是我们自己又安装了什么软件里面自带了openssl和电脑自有的发生了冲突之类的。总之就是这个家伙了。所以,我们需要重新指定一下。

# 先看看是什么版本,我的是1.0.2o_1
ls /usr/local/Cellar/openssl
# 指定切换到这个版本
brew switch openssl 1.0.2o_1
# 输出以下这样的就成功了
Cleaning /usr/local/Cellar/openssl/1.0.2o_1
Opt link created for /usr/local/Cellar/openssl/1.0.2o_1

1、问题描述

npm安装的时候报错,网上各种卸载重装node-gyp的方法试了都没用,是没找到问题根源:

gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onCpExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:305:16)
gyp ERR! stack     at emitTwo (events.js:106:13)
gyp ERR! stack     at ChildProcess.emit (events.js:191:7)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:215:12)
gyp ERR! System Darwin 16.0.0
gyp ERR! command "/usr/local/Cellar/node/8.1.3/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /Users/project/node_modules/ckmeans
gyp ERR! node -v vxx
gyp ERR! node-gyp -v vxxx
gyp ERR! not ok 

2、原因猜测

gyp是一个编译工具,类似于makefile这种命令,它主要用来编译C++的,node里面集成了它,叫做node-gyp

npm分发的都是源码,npm install的时候都是拉取源码后在本地编译,这个时候因为不可避免的,源码里面用到其他语言,比如C或者C++,所以编译的时候,就需要其他语言的编译工具比如node-gyp,但这些编译工具其实在不同系统中也是依赖不同系统的编译工具的,比如在mac中,这个node-gyp其实是依赖xcode提供的那些编译工具来具体执行的,所以我碰到的问题就在这里,是xcode的工具除了问题,xcode的工具管理是xcode-select,需要重新设置或者安装或者重置一下。

3、解决方案

具体解决方案见schnerd/d3-scale-cluster的一个issue:

# 要不要sudo看自己当前用户是不是有sudo权限
$ [sudo] xcode-select --install
xcode-select: error: command line tools are already installed, 
use "Software Update" to install updates

# 提示已经安装的话,就重置
[sudo] xcode-select --reset

问题解决。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值