ldapjs

raw JSON →
0.7.1 verified Sat May 09 auth: no javascript

LDAPjs is a pure JavaScript implementation of the LDAP protocol for Node.js, providing both client and server APIs. The current stable version is 3.0.7, released with bug fixes and security improvements. Version 3.0.0 introduced a major overhaul, splitting core LDAP operations into separate packages like @ldapjs/messages and @ldapjs/controls, and dropping support for older Node.js versions (<14). It is actively maintained, with regular releases and a focus on standards compliance and extensibility. Compared to alternatives like ldap-client or passport-ldapauth, ldapjs offers a lower-level, more flexible API suitable for building custom LDAP servers and clients.

error Cannot find module 'ldapjs'
cause ldapjs is not installed or not in node_modules.
fix
Run 'npm install ldapjs' in your project directory.
error ldap.Attribute.settings is undefined
cause Attribute.settings was added in v2.0.0; if you are on v1.x it does not exist.
fix
Upgrade to a newer version: 'npm install ldapjs@latest'
error TypeError: server.search is not a function
cause server.search is a method but may be misspelled; it expects a string DN and a handler.
fix
Check your server setup: ldap.createServer() returns a server object with .search(), .bind(), etc.
error Error: Invalid DN
cause LDAP DNs must be properly formatted (e.g., 'dc=example,dc=com').
fix
Use a valid DN string; escape special characters if needed.
breaking Version 3.0.0 dropped support for Node.js versions <14. Upgrade Node if using older versions.
fix Upgrade Node.js to v14 or later.
breaking In v3.0.0, LDAP messages and controls are now separate packages (@ldapjs/messages, @ldapjs/controls). Code that directly uses internal message types may break.
fix Install @ldapjs/messages and @ldapjs/controls as dependencies and import from those packages.
gotcha The default GUID format changed in v2.x; you must set ldap.Attribute.settings.guid_format to enable formatting, otherwise it returns raw buffer.
fix Set ldap.Attribute.settings.guid_format = ldap.GUID_FORMAT_D before accessing objectGUID.
deprecated The v2.x branch is no longer receiving updates. Users should migrate to v3.x.
fix Update to v3.x: npm install ldapjs@latest
gotcha Client search results are now Stream2 (v2+) and require listening to 'searchEntry' events; the old callback style with result.object is deprecated and may be removed.
fix Use event listeners: client.search() returns a Search object with 'searchEntry' and 'end' events.
npm install ldap
yarn add ldap
pnpm add ldap

Creates a simple LDAP server that responds to searches, and a client that binds and unbinds.

const ldap = require('ldapjs');

// Create a server
const server = ldap.createServer();

server.search('dc=example', (req, res, next) => {
  const obj = {
    dn: req.dn.toString(),
    attributes: {
      objectclass: ['organization', 'top'],
      o: 'example'
    }
  };
  if (req.filter.matches(obj.attributes)) res.send(obj);
  res.end();
});

server.listen(1389, () => {
  console.log('LDAP server listening at ' + server.url);
});

// Also create a client
const client = ldap.createClient({ url: 'ldap://localhost:1389' });
client.bind('cn=root', 'secret', (err) => {
  if (err) console.error(err);
  else console.log('bound');
});
client.unbind();