Axios Proxy-Agent Node.js working on Win but not on Macos

242 Views Asked by At

Hello I have some weird issue, Ive copied file.js from windows to macos m1, and I can't figure out why it push this error:

/Users/administrator/Desktop/NewbieCleanCode/4G_modems.js:40 httpAgent: new HttpProxyAgent(http://${proxy}), ^

TypeError: HttpProxyAgent is not a constructor at checkMyIP_v3 (/Users/administrator/Desktop/NewbieCleanCode/4G_modems.js:40:16) at Object. (/Users/administrator/Desktop/NewbieCleanCode/4G_modems.js:613:1) at Module._compile (node:internal/modules/cjs/loader:1233:14) at Module._extensions..js (node:internal/modules/cjs/loader:1287:10) at Module.load (node:internal/modules/cjs/loader:1091:32) at Module._load (node:internal/modules/cjs/loader:938:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12) at node:internal/main/run_main_module:23:47

and here is code:

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

async function checkMyIP_v3(proxy) {
  const maxRetries = 15;
  const delay = 1000;  // 1 second

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

  for (let attempt = 1; attempt <= maxRetries; attempt++) {
    try {
      const response = await client.get('http://ipv4.icanhazip.com');
      const formattedData = response.data.trim().replace(/\n/g, '');

      if (formattedData && isValidIp(formattedData)) {
        return formattedData;
      } else {
        console.log(`Invalid data received. Attempt ${attempt}.`);
      }
    } catch (error) {
      console.log('Error checking proxy. Retrying...');
    }
    await new Promise(resolve => setTimeout(resolve, delay));
  }

  console.log('Maximum retries reached.');
  return null;
}

I've npm install all same libraries and versions of axios and proxy-agents etc.. and still same. I'm little confused what I did wrong, maybe someone got ideas. I want mention that on windows same file works fine, but on macos not.

0

There are 0 best solutions below