Browse docs

Getting started with ldapjs

Install ldapjs, understand its client model, and see the shape of a minimal connection.

On this page

ldapjs is the standard LDAP client (and server) library for Node.js. This section applies the concepts from Getting Started using real, runnable code.

Install
bash
npm install ldapjs
The client model

ldapjs exposes a single Client object per connection. You create one, point it at a server URL, and then call methods on it that mirror the LDAP operations from How LDAP works: bind, search, add, modify, del, and unbind.

ts
import ldap from "ldapjs";

const client = ldap.createClient({
  url: "ldaps://dc1.example.com:636",
});

Creating a client does not immediately connect — the connection is established lazily on the first operation, and ldapjs handles reconnecting if it drops.

Callback and promise styles

ldapjs's core API is callback-based, following Node's traditional (err, result) convention:

ts
client.bind("CN=svc-app,OU=Service Accounts,DC=example,DC=com", "password", (err) => {
  if (err) {
    console.error("Bind failed:", err);
    return;
  }
  console.log("Bound successfully");
});

If you prefer promises, wrap calls with util.promisify, or use a small helper — most production codebases wrap the client once in a thin async module rather than calling the callback API directly throughout the app.

Always handle connection errors

Unlike a typical HTTP client, an ldapjs Client is long-lived and can emit error events outside of any specific operation (for example, if the underlying socket drops). Always attach an error listener:

ts
client.on("error", (err) => {
  console.error("LDAP connection error:", err);
});

Skipping this is a common source of unhandled exceptions in production ldapjs code.

What's next

Continue to Connecting for a closer look at client options, timeouts, and TLS configuration.