SOCKS5 Client for Node.js
This package provides a low-level, unopinionated SOCKS v5 client socket implementation specifically designed for Node.js. It allows developers to proxy raw TCP connections through a SOCKSv5 server by wrapping a standard `net.Socket` and performing the SOCKSv5 handshake. First published in 2012 and last updated seven years ago (version 1.2.8), its release cadence is effectively abandoned, although the package remains available and functional for its specific niche. It serves as a foundational component for other packages like `socks5-http-client` and `socks5-https-client`. Unlike more modern SOCKS client libraries, `socks5-client` focuses solely on the SOCKSv5 protocol, operates synchronously, and does not include modern JavaScript features like Promises or built-in TypeScript definitions. Its key differentiator is its minimalist approach, providing direct control over the underlying socket stream for advanced use cases.
Common errors
-
TypeError: (0 , _socks5Client.default) is not a constructor
cause Attempting to import a CommonJS-only package (`socks5-client`) using ES Module syntax (`import { SocksClient } from 'socks5-client';`) in a Node.js ESM project.fixChange your import statement to `const SocksClient = require('socks5-client');`. If using TypeScript, ensure `esModuleInterop` is true in `tsconfig.json` and use `import SocksClient = require('socks5-client');` or `import * as SocksClient from 'socks5-client';`. -
Error: connect ECONNREFUSED <proxy_host>:<proxy_port>
cause The SOCKS5 proxy server is not running, is inaccessible, or the provided `proxy_host` or `proxy_port` is incorrect.fixVerify that your SOCKS5 proxy server is operational and reachable from the machine running your Node.js application. Double-check the `PROXY_HOST` and `PROXY_PORT` configurations. Firewall rules might also be blocking the connection. -
SOCKS5 connection failed: Socks handshake failed
cause The SOCKS5 proxy initiated a handshake but failed to complete it, often due to incorrect SOCKS protocol negotiation, unsupported authentication methods, or an invalid target address/port provided during the SOCKS request.fixEnsure the SOCKS proxy is indeed a SOCKSv5 server. If using authentication, double-check `socksUsername` and `socksPassword` are correct and supported by the proxy. Verify that the `TARGET_HOST` and `TARGET_PORT` are valid and allowed by the proxy's rules. Check proxy logs for more specific error details. -
TypeError: Cannot read property 'flowing' of undefined
cause Reported issue in dependent packages (`socks5-http-client`) when used with newer Node.js versions (e.g., Node.js 14.17.0+), indicating potential compatibility breakage with stream APIs.fixThis suggests a core incompatibility. Consider downgrading Node.js if possible, or migrate to a more actively maintained SOCKS client library that is compatible with your current Node.js version. There's no direct fix within `socks5-client` itself without patches.
Warnings
- gotcha This package is CommonJS (CJS) only. Attempting to use `import` statements directly in an ESM context will lead to runtime errors (e.g., `TypeError: (0 , _socks5Client.default) is not a constructor`).
- gotcha The package has not been updated in over seven years (since version 1.2.8). This means it may not be actively maintained, could have unpatched bugs or security vulnerabilities, and might exhibit compatibility issues with newer Node.js versions or modern JavaScript features. For instance, packages depending on `socks5-client` have reported issues with Node.js versions above 14.17.0.
- gotcha The `socks5-client` library only implements basic SOCKSv5 CONNECT command functionality and 'no-authentication' or simple username/password authentication (if implemented in the original fork). It does not natively support SOCKS4/4a, UDP ASSOCIATE, or more advanced authentication methods like GSS-API.
Install
-
npm install socks5-client -
yarn add socks5-client -
pnpm add socks5-client
Imports
- SocksClient
import { SocksClient } from 'socks5-client';const SocksClient = require('socks5-client'); - SocksClient (instantiation)
const client = SocksClient(options);
const client = new SocksClient(options);
- SocksClient methods
client.connectAsync(port, host, callback);
client.connect(port, host, callback);
Quickstart
const net = require('net');
const SocksClient = require('socks5-client');
const PROXY_HOST = process.env.SOCKS5_PROXY_HOST || '127.0.0.1';
const PROXY_PORT = parseInt(process.env.SOCKS5_PROXY_PORT || '1080', 10);
const TARGET_HOST = 'example.com';
const TARGET_PORT = 80;
// Create a raw TCP socket to connect to the SOCKS5 proxy
const proxySocket = net.connect(PROXY_PORT, PROXY_HOST, () => {
console.log(`Connected to SOCKS5 proxy at ${PROXY_HOST}:${PROXY_PORT}`);
// Instantiate SocksClient with the proxy connection
const socksClient = new SocksClient();
// Initiate the SOCKS5 handshake to connect to the target host
socksClient.connect(
TARGET_PORT,
TARGET_HOST,
// Optional: add 'socksUsername', 'socksPassword' to options for auth
// { socksUsername: 'user', socksPassword: 'pass' },
proxySocket, // Pass the connected proxySocket
(err, destinationSocket) => {
if (err) {
console.error('SOCKS5 connection failed:', err.message);
proxySocket.end();
return;
}
console.log(`Successfully connected to ${TARGET_HOST}:${TARGET_PORT} via SOCKS5 proxy.`);
// Now destinationSocket is the proxied socket, ready for application data
const request = [
'GET / HTTP/1.1',
`Host: ${TARGET_HOST}`,
'Connection: close',
'',
''
].join('\r\n');
destinationSocket.write(request);
destinationSocket.on('data', (data) => {
console.log('Received data from target:', data.toString().substring(0, 200) + '...');
});
destinationSocket.on('end', () => {
console.log('Target connection ended.');
proxySocket.end();
});
destinationSocket.on('error', (data) => {
console.error('Target socket error:', data.message);
proxySocket.end();
});
}
);
});
proxySocket.on('error', (err) => {
console.error('Proxy socket error:', err.message);
});