本文主要是介绍npm打包报错解决办法--因网络问题,node-gyp依赖的node-headers无法下载,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、报错信息
下载 https://nodejs.org/download/release/v16.13.0/node-v16.13.0-headers.tar.gz 超时,导致打包失败。
- request to https://nodejs.org/download/release/v16.13.0/node-v16.13.0-headers.tar.gz failed, reason: connect ETIMEDOUT 104.20.22.46:443
13:30:34 npm ERR! command failed
13:30:34 npm ERR! command sh -c node ./build.js
13:30:34 npm ERR! gyp info it worked if it ends with ok
13:30:34 npm ERR! gyp info using node-gyp@8.2.0
13:30:34 npm ERR! gyp info using node@16.13.0 | linux | x64
13:30:34 npm ERR! gyp info find Python using Python version 3.7.3 found at "/usr/bin/python3"
13:30:34 npm ERR! gyp WARN EACCES current user ("node") does not have permission to access the dev dir "/root/.cache/node-gyp/16.13.0"
13:30:34 npm ERR! gyp WARN EACCES attempting to reinstall using temporary dev dir "/tmp/.node-gyp"
13:30:34 npm ERR! gyp http GET https://nodejs.org/download/release/v16.13.0/node-v16.13.0-headers.tar.gz
13:30:34 npm ERR! (node:195) Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0' makes TLS connections and HTTPS requests insecure by disabling certificate verification.
13:30:34 npm ERR! (Use `node --trace-warnings ...` to show where the warning was created)
13:30:34 npm ERR! gyp WARN install got an error, rolling back install
13:30:34 npm ERR! gyp ERR! configure error
13:30:34 npm ERR! gyp ERR! stack FetchError: request to https://nodejs.org/download/release/v16.13.0/node-v16.13.0-headers.tar.gz failed, reason: connect ETIMEDOUT 104.20.23.46:443
13:30:34 npm ERR! gyp ERR! stack at ClientRequest.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/minipass-fetch/lib/index.js:110:14)
13:30:34 npm ERR! gyp ERR! stack at ClientRequest.emit (node:events:390:28)
13:30:34 npm ERR! gyp ERR! stack at TLSSocket.socketErrorListener (node:_http_client:447:9)
13:30:34 npm ERR! gyp ERR! stack at TLSSocket.emit (node:events:402:35)
13:30:34 npm ERR! gyp ERR! stack at emitErrorNT (node:internal/streams/destroy:157:8)
13:30:34 npm ERR! gyp ERR! stack at emitErrorCloseNT (node:internal/streams/destroy:122:3)
13:30:34 npm ERR! gyp ERR! stack at processTicksAndRejections (node:internal/process/task_queues:83:21)
13:30:34 npm ERR! gyp ERR! System Linux 4.15.0-213-generic
13:30:34 npm ERR! gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
13:30:34 npm ERR! gyp ERR! cwd /home/jenkins/agent/workspace/TEST_gov_h5_eavservice-activity/node_modules/deasync
13:30:34 npm ERR! gyp ERR! node -v v16.13.0
13:30:34 npm ERR! gyp ERR! node-gyp -v v8.2.0
13:30:34 npm ERR! gyp ERR! not ok
13:30:34 npm ERR! Build failed
13:30:34 npm timing npm Completed in 268479ms
二、什么是node-headers
node-v16.13.0-headers.tar.gz 文件是 Node.js 的 C/C++ 头文件包,它包含了 Node.js 运行时所需的所有头文件。这个文件通常用于以下场景:
-
构建原生模块:如果你在使用 Node.js 构建或安装需要编译原生代码的模块(例如,使用 node-gyp 构建的模块),这个头文件包是必须的。
-
开发 Node.js 插件:开发者在开发 Node.js 的 C/C++ 插件时,需要这些头文件来编译他们的代码。
默认下载地址是https://nodejs.org/download/release/v16.13.0/node-v16.13.0-headers.tar.gz,由于公司网络受限,无法下载,导致h5项目打包失败。
那么有什么好的解决办法呢?
- 在打包的机器上,建立网络代理,解决网络问题
- 手动下载到本地,然后gyp构建依赖本地,不从远程下载
- 重定向node-v16.13.0-headers.tar.gz文件的地址为内网地址
三、解决方案
下载node-v16.13.0-headers.tar.gz,并上传到minio,jenkins打包的时候,从minio下载头文件,不从外网下载。
- 创建文件夹v16.13.0, 见下
- 上传文件
两个文件的下载地址分别是: - http://192.168.8.18:9000/xh-res/v16.13.0/node-v16.13.0-headers.tar.gz
- http://192.168.8.18:9000/xh-res/v16.13.0/SHASUMS256.txt
四、下载文件SHASUMS256.txt出现404错误
文件node-v16.13.0-headers.tar.gz下载成功,返回http状态码为200。
但是还缺少映射一个文件SHASUMS256.txt,导致出现404错误。
所以,我又补充上传了文件SHASUMS256.txt。
16:36:37 npm ERR! code 1
16:36:37 npm ERR! path /home/jenkins/agent/workspace/TEST_gov_h5_eavservice-activity/node_modules/deasync
16:36:37 npm ERR! command failed
16:36:37 npm ERR! command sh -c node ./build.js
16:36:37 npm ERR! gyp info it worked if it ends with ok
16:36:37 npm ERR! gyp info using node-gyp@8.2.0
16:36:37 npm ERR! gyp info using node@16.13.0 | linux | x64
16:36:37 npm ERR! gyp info find Python using Python version 3.7.3 found at "/usr/bin/python3"
16:36:37 npm ERR! gyp WARN EACCES current user ("node") does not have permission to access the dev dir "/root/.cache/node-gyp/16.13.0"
16:36:37 npm ERR! gyp WARN EACCES attempting to reinstall using temporary dev dir "/tmp/.node-gyp"
16:36:37 npm ERR! gyp http GET http://192.168.8.18:9000/xh-res/v16.13.0/node-v16.13.0-headers.tar.gz
16:36:37 npm ERR! gyp http 200 http://192.168.8.18:9000/xh-res/v16.13.0/node-v16.13.0-headers.tar.gz
16:36:37 npm ERR! gyp http GET http://192.168.8.18:9000/xh-res/v16.13.0/SHASUMS256.txt
16:36:37 npm ERR! gyp http 404 http://192.168.8.18:9000/xh-res/v16.13.0/SHASUMS256.txt
16:36:37 npm ERR! gyp WARN install got an error, rolling back install
16:36:37 npm ERR! gyp ERR! configure error
16:36:37 npm ERR! gyp ERR! stack Error: 404 status code downloading checksum
16:36:37 npm ERR! gyp ERR! stack at downloadShasums (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/install.js:214:15)
16:36:37 npm ERR! gyp ERR! stack at processTicksAndRejections (node:internal/process/task_queues:96:5)
16:36:37 npm ERR! gyp ERR! stack at async Promise.all (index 1)
16:36:37 npm ERR! gyp ERR! stack at async go (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/install.js:189:5)
16:36:37 npm ERR! gyp ERR! stack at async install (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/install.js:62:18)
16:36:37 npm ERR! gyp ERR! System Linux 4.15.0-213-generic
16:36:37 npm ERR! gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
16:36:37 npm ERR! gyp ERR! cwd /home/jenkins/agent/workspace/TEST_gov_h5_eavservice-activity/node_modules/deasync
16:36:37 npm ERR! gyp ERR! node -v v16.13.0
16:36:37 npm ERR! gyp ERR! node-gyp -v v8.2.0
16:36:37 npm ERR! gyp ERR! not ok
16:36:37 npm ERR! Build failed
16:36:37 npm timing npm Completed in 137987ms
五、修改Dockerfile
# 设置 node-gyp 寻找头文件和库文件的路径
# xh-res 是minio的bucket名称
ENV NODEJS_ORG_MIRROR "http://192.168.8.18:9000/xh-res"
六、总结
至此,node-gyp打包就顺畅无阻了。
期间,我尝试在docker容器里,下载头文件node-v16.13.0-headers.tar.gz,然后通过环境变量NODEJS_ORG_MIRROR指向它。
最大的失误就是,把node-v16.13.0-headers.tar.gz解压出来了,其实是不能解压的。
但这是绝大多数ai机器人会给你的答案,之所以写这篇文章,希望看到这篇文章的朋友可以少走弯路。
另外,NODEJS_ORG_MIRROR 配置的是域名,完整的地址会再追加v16.13.0/node-v16.13.0-headers.tar.gz和v16.13.0/SHASUMS256.txt。(地址调试几遍,就没有什么问题了)
这篇关于npm打包报错解决办法--因网络问题,node-gyp依赖的node-headers无法下载的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!