You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Axios Proxy-Agent在Windows正常但MacOS报错:HttpProxyAgent不是构造函数

问题解决思路

核心原因

报错TypeError: HttpProxyAgent is not a constructor本质是模块导入方式不匹配,或是M1 Mac(ARM架构)与Windows(x86架构)的依赖编译/解析差异导致无法正确获取构造函数。

具体解决步骤

1. 修正模块导入语句

新版本的http-proxy-agent和https-proxy-agent可能采用命名导出而非默认导出,修改导入代码为:

const ProxyAgent = require('proxy-agent');
const { HttpProxyAgent } = require('http-proxy-agent');
const { HttpsProxyAgent } = require('https-proxy-agent');

如果仍报错,尝试取默认导出:

const HttpProxyAgent = require('http-proxy-agent').default;
const HttpsProxyAgent = require('https-proxy-agent').default;

2. 清理并重新安装依赖

M1 Mac的ARM架构与Windows的x86架构存在预编译二进制差异,即使版本相同,缓存的依赖文件可能不兼容:

  • 执行命令删除旧依赖和锁文件:
    rm -rf node_modules
    rm package-lock.json
    
  • 重新安装依赖:
    npm install
    

3. 统一Node.js版本

检查Windows和Mac上的Node.js版本是否一致(执行node -v查看),不同版本的模块解析规则可能存在差异,将Mac上的Node版本调整为与Windows一致。

4. 简化代理实现(备选方案)

代码中已导入proxy-agent,它可以自动处理HTTP/HTTPS代理,无需区分两类代理类,直接替换原有代码:

const client = axios.create({
  timeout: 30000,
  httpAgent: new ProxyAgent(`http://${proxy}`),
  httpsAgent: new ProxyAgent(`http://${proxy}`),
});

内容的提问来源于stack exchange,提问作者Bella Smith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 12:11:24