LDAP.js Client

0.1.7 · active · verified Tue Apr 21

ldapjs-client is a JavaScript LDAP client library designed for modern Node.js environments, requiring Node.js >= 8.0. It offers a promise-based API for asynchronous interactions with LDAP servers, supporting common operations such as adding, binding, deleting, modifying, renaming (modifyDN), and searching for entries. A primary distinguishing feature of `ldapjs-client` is its intentional independence from the `ldapjs` package, which has been unmaintained for several years. This package positions itself as an actively supported alternative, addressing the need for robust LDAP client functionality in contemporary Node.js applications. The current stable version is 0.1.7. While specific release cadences are not detailed, the presence of continuous integration workflows suggests ongoing development and maintenance.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create an LDAP client, perform a basic search operation, and properly close the connection using asynchronous promise-based syntax.

import LdapClient from 'ldapjs-client';

const client = new LdapClient({
  url: 'ldap://127.0.0.1:389',
  timeout: 5000 // 5-second timeout for operations
});

async function performLdapSearch() {
  try {
    // Assuming an anonymous bind is allowed for search, or bind first
    // await client.bind('admin_user', process.env.LDAP_PASSWORD ?? '');

    const options = {
      filter: '(&(objectclass=person)(cn=*))', // Search for any person entry
      scope: 'sub',
      attributes: ['dn', 'cn', 'mail']
    };
    console.log('Searching LDAP...');
    const entries = await client.search('o=example', options);
    console.log(`Found ${entries.length} entries:`)
    entries.forEach(entry => console.log(entry.dn, entry.cn, entry.mail));

    // Clean up connection
    await client.unbind();
    await client.destroy();
  } catch (e) {
    console.error('LDAP operation failed:', e.message);
    // Ensure client is destroyed even on error
    if (client) await client.destroy().catch(err => console.error('Error destroying client:', err.message));
  }
}

performLdapSearch();

view raw JSON →