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.
npm install socks-proxy-agentVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to create a `SocksProxyAgent` instance and use it with Node.js's built-in `https` module to route an HTTPS request through a SOCKS proxy, including basic error handling.
Ensure your project runs on Node.js 20 or newer. If not possible, use `socks-proxy-agent@9.x.x` which supports Node.js 16+.
Migrate your project to use ES Modules (`import`/`export` syntax) or configure your build system (e.g., Webpack, Rollup) to handle ESM. For testing, `node --experimental-modules` might be needed in older Node.js versions.
Format your proxy URI as `socks://username:password@host:port`. Ensure `username` and `password` are `encodeURIComponent()`-ed if they contain characters like `@`, `:`, `/`, etc.
Pass the `agent` instance in the options object: `new WebSocket('ws://...', { agent: yourSocksProxyAgentInstance });`Ensure the proxy URI starts with a valid SOCKS scheme (e.g., `socks://`, `socks5h://`). If you need to handle multiple proxy types dynamically, consider using the `proxy-agent` package from the same monorepo, which automatically detects the protocol.
Change 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`).Ensure you are instantiating the class correctly: `const agent = new SocksProxyAgent(proxyUri);`
Verify 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`).
Check 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.