ldapjs is the standard LDAP client (and server) library for Node.js. This section applies the concepts from Getting Started using real, runnable code.
npm install ldapjs
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.
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.
ldapjs's core API is callback-based, following Node's traditional (err, result) convention:
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.
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:
client.on("error", (err) => {
console.error("LDAP connection error:", err);
});
Skipping this is a common source of unhandled exceptions in production ldapjs code.
Continue to Connecting for a closer look at client options, timeouts, and TLS configuration.