Mobile Proxy Node.js Setup
Step-by-step mobile proxy Node.js setup tutorial. Configure axios, node-fetch, Puppeteer, and Playwright with dedicated 4G/5G carrier IPs for reliable JavaScript automation.
- https-proxy-agent integration — route axios and native fetch requests through authenticated HTTP tunnels.
- socks-proxy-agent support — full SOCKS5 protocol coverage with remote DNS resolution in modern Node.js runtimes.
- Puppeteer page authentication — handle proxy credential prompts using page.authenticate() or Chrome runtime flags.
- Playwright context isolation — assign dedicated mobile proxies per browser context for leak-free multi-accounting.
Node.js Proxy Options
Unlike browser runtimes or Python, Node.js does not automatically read HTTP_PROXY environment variables out of the box for built-in HTTP requests. In modern JavaScript and TypeScript backends, routing outbound traffic through a proxy requires configuring an Agent dispatcher or passing network parameters into high-level browser automation frameworks.
| Tool / Library | Proxy Method | Authentication Handling | Use Case |
|---|---|---|---|
| axios | https-proxy-agent | Inline URL credentials | REST APIs, backend microservices |
| fetch (Node 18+) | undici ProxyAgent | ProxyAgent basic auth | Standard web APIs, Next.js backends |
| Puppeteer | args + page.authenticate | CDP event intercept | Headless Chrome automation |
| Playwright | Context proxy config | Native server object | Modern browser scraping & tests |
axios and node-fetch Setup
When using axios, developers frequently attempt to pass a basic { host, port, auth } proxy object. While this works for plaintext HTTP, it often causes connection reset errors on HTTPS targets. The industry standard solution is to install https-proxy-agent or socks-proxy-agent to establish clean HTTP CONNECT tunnels.
import axios from 'axios';
import { HttpsProxyAgent } from 'https-proxy-agent';
// Define dedicated PXM2 mobile proxy endpoint
const proxyUrl = 'http://user123:secret456@proxy.pxm2.io:8000';
const httpsAgent = new HttpsProxyAgent(proxyUrl);
// Create reusable axios client instance
const client = axios.create({
httpsAgent,
timeout: 15000,
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
},
});
async function checkCarrierIp() {
try {
const response = await client.get('https://ipinfo.io/json');
console.log('Connected through mobile IP:', response.data.ip);
} catch (err) {
console.error('Proxy request failed:', err.message);
}
}
checkCarrierIp();
For SOCKS5 proxies in Node.js, install socks-proxy-agent and initialize SocksProxyAgent('socks5h://user:pass@proxy.pxm2.io:1080'). The socks5h protocol ensures remote DNS resolution without client-side leaks.
Puppeteer Proxy Configuration
Puppeteer launches an external Chromium binary. Because Chrome does not accept inline user and password credentials in its command line arguments, Puppeteer requires a two-step process: launching the browser with --proxy-server and calling page.authenticate() to supply credentials over the Chrome DevTools Protocol (CDP).
import puppeteer from 'puppeteer';
async function runBrowser() {
const browser = await puppeteer.launch({
headless: 'new',
args: [
// 1. Pass host and port to Chrome flags
'--proxy-server=http://proxy.pxm2.io:8000',
'--no-sandbox',
],
});
const page = await browser.newPage();
// 2. Intercept 407 authentication challenges
await page.authenticate({
username: 'user123',
password: 'secret456',
});
await page.goto('https://ipinfo.io', { waitUntil: 'domcontentloaded' });
const ip = await page.$eval('body', el => el.innerText);
console.log('Puppeteer IP verified:', ip);
await browser.close();
}
runBrowser();
Playwright Proxy Setup
Playwright provides cleaner proxy ergonomics than Puppeteer. Instead of relying on CDP authentication interception, Playwright allows you to define proxy settings at the context level. This makes it possible to assign different mobile proxies to separate browser contexts concurrently inside a single Chromium instance.
import { chromium } from 'playwright';
async function main() {
const browser = await chromium.launch({ headless: true });
// Create isolated context with dedicated mobile proxy
const context = await browser.newContext({
proxy: {
server: 'http://proxy.pxm2.io:8000',
username: 'user123',
password: 'secret456',
},
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36',
});
const page = await context.newPage();
await page.goto('https://api.ipify.org?format=json');
console.log(await page.textContent('body'));
await context.close();
await browser.close();
}
main();
Node.js Proxy Best Practices
When running Node.js scrapers and bots at scale, proper connection pooling and memory hygiene are vital:
-
1. Enable Keep-Alive on Proxy AgentsAlways set keepAlive: true in your HttpsProxyAgent options. Reusing persistent TCP sockets saves 100ms–250ms of cellular latency on every sequential request.
-
2. Close Idle ContextsIn Playwright and Puppeteer, never leave unused browser contexts or pages open. Lingering contexts consume memory and keep open socket connections to the cellular modem.
-
3. Handle On-Demand Rotation DelaysWhen triggering an IP rotation via the PXM2 webhook in Node.js, wrap the rotation call in a Promise that pauses for 6 seconds before resolving your request queue.
France
India
Poland
Frequently Asked Questions
How do I use a mobile proxy with axios in Node.js?
In modern axios, use https-proxy-agent or socks-proxy-agent to create an httpAgent or httpsAgent and pass it to your axios instance. This bypasses issues with basic proxy object configurations and ensures full CONNECT tunnel support for HTTPS.
How does Playwright handle proxy authentication?
Playwright supports proxy authentication directly in browser.newContext({ proxy: { server, username, password } }) or at browser launch. Unlike Puppeteer, Playwright handles authentication at the network protocol layer without requiring page-level popups.
Can I use mobile proxies with the native Node.js fetch API?
Yes. In Node.js 18 and newer, global fetch uses Undici. You can configure a proxy by passing a custom Dispatcher instance from undici (ProxyAgent), or by setting the standard HTTP_PROXY environment variables with undici-fetch.
Why do WebSocket connections fail over Node.js HTTP proxies?
WebSockets require an HTTP 101 Switching Protocols upgrade. When routing WebSockets through an HTTP proxy, ensure your proxy agent supports the HTTP CONNECT method, or switch to a SOCKS5 proxy which tunnels raw TCP streams without inspecting payload headers.
How do I manage mobile proxy IP rotation in Node.js scripts?
When rotating via API, execute an HTTP GET request to your PXM2 rotation URL. Use a short delay of 5 to 10 seconds to allow the cellular modem to establish a new carrier session, then execute an IP check against https://api.ipify.org before continuing.
Related Mobile Proxy Guides
Explore related developer tutorials, language integrations, and mobile proxy infrastructure guides.
Developer Guides
Platform & Scraping Guides
Accelerate Your Node.js Automation with Carrier Proxies
Dedicated 4G/5G mobile modems with unlimited data, low latency, and instantaneous API-driven IP rotation.
Get a Mobile Proxy