Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Node.js · Module

node:net

Node.js’s node:net module provides asynchronous, stream-based TCP and IPC networking. Use it to create servers with net.createServer() and clients with net.createConnection() or net.connect().

How it works

The node:net module supplies asynchronous stream-based networking for TCP and IPC. Servers are created with net.createServer(), while clients use net.createConnection() or its alias net.connect(); sockets provide the connection’s readable and writable stream interface.

Syntax

import net from 'node:net';
net.connect(options[, connectListener]);
net.connect(path[, connectListener]);
net.connect(port[, host][, connectListener]);

Parameters

NameTypeDescription
optionsobjectConnection options, including TCP or IPC addressing settings.
pathstringIPC endpoint path.
portnumberTCP destination port.
hoststringTCP destination host; optional in the port overload.
connectListenerfunctionFunction invoked after the connection is established.

Returns

A new net.Socket that starts the connection.

Key points

  • The module is asynchronous and centers on stream-based networking.
  • It supports both TCP and IPC, with separate server and client factory functions.
  • Import it with the modern node:net specifier; CommonJS access is also documented.
  • net.connect() is an alias for net.createConnection().
  • The connection factory returns a socket and starts connecting immediately.

Common mistakes

  • Do not treat path as a TCP host or port; it identifies an IPC endpoint for these APIs.

Example: TCP echo server and client

import net from 'node:net';

const server = net.createServer((socket) => {
  socket.on('data', (chunk) => socket.write(chunk));
});

server.listen(0, '127.0.0.1', () => {
  const address = server.address();
  if (!address || typeof address === 'string') return;
  const client = net.createConnection(address.port, address.address, () => {
    client.write('hello');
  });
  client.on('data', (data) => {
    console.log(data.toString());
    client.end();
    server.close();
  });
});

Creates a local TCP server that echoes client data, then connects to it and prints the response.

When to use it

TCP servers

Build a lightweight TCP service that accepts connections and exchanges stream data.

TCP clients

Connect to another TCP service from a Node.js process.

IPC networking

Communicate locally through operating-system IPC endpoints.

Interview questions

QWhat does the node:net module provide?

The module provides asynchronous networking for stream-based TCP or IPC servers and clients. net.createServer() creates servers, and net.createConnection() creates clients.

Follow-up: How can a client connect without explicitly calling socket.connect()? Use net.connect() or net.createConnection(), which creates a socket and immediately initiates the connection.

QWhat are the principal forms of net.connect()?

net.connect() is an alias for net.createConnection(). It accepts options, an IPC path, or a TCP port with an optional host and connection listener.

Follow-up: Which argument identifies an IPC endpoint? The path argument does, and it is accepted by connection, listening, and socket-connection APIs.

QHow are TCP servers and clients started?

A server is started with server.listen(), and a client socket is established with socket.connect() or the connection factory functions. The resulting socket participates in stream-based I/O.

Follow-up: What does the factory return? It returns a new net.Socket that has begun connecting.

  • net.createServer()
  • net.createConnection()
  • net.Server
  • net.Socket
  • server.listen()
  • socket.connect()

Source: Net | Node.js v26.11.1 Documentation. Checked 2026-10-15.