SOCKS Proxy HTTP/HTTPS Agent
socks-proxy-agent is a Node.js module that provides an `http.Agent` implementation, enabling HTTP and HTTPS requests, and WebSocket connections, to be routed through a SOCKS proxy server. It supports SOCKS4, SOCKS4a, SOCKS5, and SOCKS5h protocols, offering flexibility in how connections are established and resolved. The current stable version is 10.0.0. The package is part of the broader `proxy-agents` monorepo by TooTallNate, which implies a release cadence often synchronized with its core dependency `agent-base` and Node.js LTS updates, as evidenced by the recent major version bump due to an increased minimum Node.js requirement. Its key differentiator is its focused and robust implementation for SOCKS proxies, making it suitable for scenarios requiring specific proxy tunneling capabilities for anonymity, bypassing geo-restrictions, or internal network access.
Common errors
-
Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/socks-proxy-agent/dist/index.js from ... not supported.
cause Attempting to `require()` `socks-proxy-agent` in a CommonJS context when the library is ESM-only since v7.0.0.fixChange your import statement to `import { SocksProxyAgent } from 'socks-proxy-agent';` and ensure your project is configured for ES Modules (e.g., `"type": "module"` in `package.json`). -
TypeError: The agent option must be an Agent object, got [object Object]
cause Instantiating `SocksProxyAgent` without the `new` keyword, or passing an incorrectly configured object as an agent.fixEnsure you are instantiating the class correctly: `const agent = new SocksProxyAgent(proxyUri);` -
Error: SOCKS proxy connection failed: Authentication failed
cause The SOCKS proxy server rejected the provided username/password credentials, or they were not properly URL-encoded.fixVerify that the username and password in your SOCKS proxy URI are correct and properly URL-encoded (e.g., `user%40domain.com` for `user@domain.com`). -
Error: getaddrinfo ENOTFOUND proxy.example.com
cause The hostname of the SOCKS proxy server could not be resolved by DNS.fixCheck the proxy server hostname for typos. Ensure your system's DNS settings are correct, or that the proxy hostname is reachable. This can also happen if `socks5://` is used and the proxy expects `socks5h://` for remote DNS resolution.
Warnings
- breaking Version 10.0.0 increased the minimum Node.js version requirement to 20. Projects running on older Node.js versions will need to upgrade their runtime or stick to an earlier `socks-proxy-agent` version.
- breaking Starting with `socks-proxy-agent@7.0.0` (as part of the `proxy-agents` monorepo v7), the package is ESM-only. CommonJS `require()` statements will no longer work directly.
- gotcha Proxy authentication (username and password) must be included directly within the SOCKS proxy URI and URL-encoded if special characters are present. Incorrect encoding or format will lead to authentication failures.
- gotcha When using `socks-proxy-agent` with the `ws` (WebSocket) library, the `agent` option must be passed explicitly to the `WebSocket` constructor. This is a common oversight leading to direct connections instead of proxied ones.
- gotcha The `SocksProxyAgent` expects a SOCKS URI (e.g., `socks://`, `socks4://`, `socks5://`). Providing an HTTP/HTTPS proxy URI (e.g., `http://`) will result in incorrect behavior or errors as it's designed specifically for SOCKS protocols.
Install
-
npm install socks-proxy-agent -
yarn add socks-proxy-agent -
pnpm add socks-proxy-agent
Imports
- SocksProxyAgent
const SocksProxyAgent = require('socks-proxy-agent').SocksProxyAgent;import { SocksProxyAgent } from 'socks-proxy-agent'; - SocksProxyAgentOptions
import { SocksProxyAgentOptions } from 'socks-proxy-agent';import type { SocksProxyAgentOptions } from 'socks-proxy-agent'; - Agent
const agent = SocksProxyAgent(proxyUri);
import https from 'https'; // or http const agent = new SocksProxyAgent(proxyUri);
Quickstart
import https from 'https';
import { SocksProxyAgent } from 'socks-proxy-agent';
// Replace with your SOCKS proxy URI, including optional authentication
// Example with username/password: 'socks://user%40example.com:password@proxy.example.com:1080'
const SOCKS_PROXY_URI = process.env.SOCKS_PROXY_URI ?? 'socks://127.0.0.1:1080';
// Create a new SocksProxyAgent instance
const agent = new SocksProxyAgent(SOCKS_PROXY_URI);
console.log(`Making HTTPS request through SOCKS proxy: ${SOCKS_PROXY_URI}`);
https.get('https://ipinfo.io/json', { agent }, (res) => {
console.log(`Response Status: ${res.statusCode}`);
console.log('Response Headers:', res.headers);
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
console.log('Response Body:');
try {
const json = JSON.parse(data);
console.log(json);
} catch (e) {
console.error('Failed to parse JSON response:', e);
console.log(data);
}
});
}).on('error', (err) => {
console.error('Error making HTTPS request:', err.message);
});