Both activedirectory and ldapjs are Node.js packages used to interact with LDAP servers, such as Microsoft Active Directory. activedirectory is a higher-level wrapper designed specifically for Active Directory environments, simplifying common tasks like user authentication and group lookup. ldapjs is a lower-level, protocol-focused client that implements the LDAP v3 specification directly, offering granular control over connections, searches, and modifications. While activedirectory aims for ease of use, ldapjs provides the foundation many other libraries build upon.
Both activedirectory and ldapjs enable Node.js applications to communicate with directory services like Microsoft Active Directory. They solve the same core problem — verifying user credentials and fetching directory data — but they approach it with different levels of abstraction and maintenance status. Let's compare how they handle real-world engineering tasks.
activedirectory is considered legacy software.
// activedirectory: Legacy callback patterns
const ActiveDirectory = require('activedirectory');
const ad = new ActiveDirectory(config);
// Risk: No recent security patches or Node.js version updates
ldapjs is actively maintained.
// ldapjs: Modern maintenance
const ldap = require('ldapjs');
const client = ldap.createClient({ url: 'ldap://localhost' });
// Benefit: Active security patches and community support
activedirectory wraps configuration in a simple object.
// activedirectory: Simplified config
const config = {
url: 'ldaps://dc.example.com',
baseDN: 'dc=example,dc=com',
username: 'admin@example.com',
password: 'secret'
};
const ad = new ActiveDirectory(config);
ldapjs requires explicit client creation.
// ldapjs: Explicit client setup
const client = ldap.createClient({
url: 'ldaps://dc.example.com',
tlsOptions: { rejectUnauthorized: true }
});
client.bind('admin@example.com', 'secret', (err) => { /*...*/ });
activedirectory provides a dedicated authenticate method.
// activedirectory: One-step auth
ad.authenticate(username, password, (err, auth) => {
if (err) return console.error(err);
if (auth) console.log('Login successful');
});
ldapjs uses the standard LDAP bind operation.
// ldapjs: Manual bind flow
const dn = `cn=${username},ou=users,dc=example,dc=com`;
client.bind(dn, password, (err) => {
if (!err) console.log('Login successful');
client.unbind();
});
activedirectory uses helper methods for common searches.
findUser simplify looking up specific entries.// activedirectory: Helper methods
ad.findUser(username, (err, user) => {
if (err) return console.error(err);
console.log(user.mail);
});
ldapjs uses generic search with filters.
// ldapjs: Generic search
const opts = {
filter: `(sAMAccountName=${username})`,
scope: 'sub'
};
client.search('dc=example,dc=com', opts, (err, res) => {
res.on('searchEntry', (entry) => console.log(entry.object));
});
activedirectory wraps errors in a custom format.
// activedirectory: Wrapped errors
ad.authenticate(user, pass, (err, auth) => {
if (err) {
// Generic error object
console.error(err.message);
}
});
ldapjs exposes raw LDAP errors.
// ldapjs: Protocol errors
client.bind(dn, pass, (err) => {
if (err) {
// Specific LDAP error codes
console.error(err.code, err.message);
}
});
activedirectory lacks official type definitions.
.d.ts files or use any.// activedirectory: No types
// @ts-ignore
const ad = new ActiveDirectory(config);
// Developer must guess method signatures
ldapjs has community-driven type support.
@types/ldapjs exist.// ldapjs: Type support available
import * as ldap from 'ldapjs';
const client: ldap.Client = ldap.createClient({ url: '...' });
// Editor autocomplete and type checking work
While the implementation differs, both packages share core LDAP capabilities.
// Both support secure connections
// activedirectory
const ad = new ActiveDirectory({ url: 'ldaps://...' });
// ldapjs
const client = ldap.createClient({ url: 'ldaps://...' });
// activedirectory
ad.findUser('john', (err, user) => { /*...*/ });
// ldapjs
client.search('dc=...', { filter: '(cn=john)' }, (err, res) => { /*...*/ });
// Express middleware example (conceptual)
app.post('/login', (req, res) => {
// Use either package here to verify req.body.password
});
| Feature | activedirectory | ldapjs |
|---|---|---|
| Maintenance | ⚠️ Legacy / Inactive | ✅ Active / Standard |
| Abstraction | 🧩 High (AD specific) | 🔧 Low (Protocol focused) |
| Auth Method | 🔑 .authenticate() | 🔑 .bind() |
| Search | 🔍 Helper methods | 🔍 Raw filters |
| TypeScript | ❌ Poor support | ✅ Community types available |
| Use Case | 🕰️ Legacy maintenance | 🚀 New development |
activedirectory is like an old utility knife 🔪 — it works for specific tasks you've always done, but the blade is dulling. It simplifies Active Directory interactions but carries the risk of unmaintained code. Use it only if you are stuck maintaining an older system.
ldapjs is like a precision tool set 🧰 — it requires more skill to use but handles any job you throw at it. It is the industry standard for LDAP in Node.js and ensures your authentication layer remains secure and up-to-date.
Final Thought: For any new project, ldapjs is the clear choice. Security and maintenance matter more than minor convenience gains. If you need TypeScript support, also consider ldapts, a modern fork of ldapjs built with types in mind.
Choose activedirectory only if you are maintaining a legacy system that already depends on it and requires minimal changes. It simplifies basic Active Directory tasks but has not seen significant updates in years. Avoid using it for new projects due to potential security risks and lack of modern feature support.
Choose ldapjs for any new development requiring LDAP integration. It is actively maintained, supports modern Node.js versions, and gives you full control over the LDAP protocol. It is the better choice for complex queries, custom authentication flows, and long-term project stability.
ActiveDirectory is an ldapjs client for authN (authentication) and authZ (authorization) for Microsoft Active Directory with range retrieval support for large Active Directory installations. This code was a port of an existing C# library (not published) that I had written a few years ago. Here are the key features
ActiveDirectory uses the following additional node modules:
npm install activedirectory
var ActiveDirectory = require('activedirectory');
var config = { url: 'ldap://dc.domain.com',
baseDN: 'dc=domain,dc=com',
username: 'username@domain.com',
password: 'password' }
var ad = new ActiveDirectory(config);
The username and password specified in the configuration are what are used for user and group lookup operations.
Authenticates the username and password by doing a simple bind with the specified credentials.
Arguments
Example
var ad = new ActiveDirectory(config);
var username = 'john.smith@domain.com';
var password = 'password';
ad.authenticate(username, password, function(err, auth) {
if (err) {
console.log('ERROR: '+JSON.stringify(err));
return;
}
if (auth) {
console.log('Authenticated!');
}
else {
console.log('Authentication failed!');
}
});
Checks to see if a user is a member of the specified group. This function will also check for group membership inside of a group. Even if a user is not explicity listed as a member of a particular group, if a group that the user is a member of belongs to the group, then this function will return true.
Arguments
Example
var username = 'user@domain.com';
var groupName = 'Employees';
var ad = new ActiveDirectory(config);
var ad.isUserMemberOf(username, groupName, function(err, isMember) {
if (err) {
console.log('ERROR: ' +JSON.stringify(err));
return;
}
console.log(username + ' isMemberOf ' + groupName + ': ' + isMember);
});
Checks to see if the specified group exists.
Arguments
Example
var groupName = 'Employees';
var ad = new ActiveDirectory(config);
ad.groupExists(groupName, function(err, exists) {
if (err) {
console.log('ERROR: ' +JSON.stringify(err));
return;
}
console.log(groupName + ' exists: ' + exists);
});
Checks to see if the specified user exists.
Arguments
Example
var username = 'john.smith@domain.com';
var ad = new ActiveDirectory(config);
ad.userExists(username, function(err, exists) {
if (err) {
console.log('ERROR: ' +JSON.stringify(err));
return;
}
console.log(username + ' exists: ' + exists);
});
For the specified group, retrieve all of the users that belong to the group. If the group contains groups, then the members of those groups are recursively retrieved as well to build a complete list of users that belong to the specified group.
Arguments
Example
var groupName = 'Employees';
var ad = new ActiveDirectory(config);
ad.getUsersForGroup(groupName, function(err, users) {
if (err) {
console.log('ERROR: ' +JSON.stringify(err));
return;
}
if (! users) console.log('Group: ' + groupName + ' not found.');
else {
console.log(JSON.stringify(users));
}
});
For the specified username, retrieve all of the groups that a user belongs to. If a retrieved group is a member of another group, then that group is recursively retrieved as well to build a complete hierarchy of groups that a user belongs to.
Arguments
Example
var sAMAccountName = 'john.smith@domain.com';
var ad = new ActiveDirectory(config);
ad.getGroupMembershipForUser(sAMAccountName, function(err, groups) {
if (err) {
console.log('ERROR: ' +JSON.stringify(err));
return;
}
if (! groups) console.log('User: ' + sAMAccountName + ' not found.');
else console.log(JSON.stringify(groups));
});
For the specified group, retrieve all of the groups that the group is a member of. If a retrieved group is a member of another group, then that group is recursively retrieved as well to build a complete hierarchy of groups that a user belongs to.
Arguments
Example
var groupName = 'Employees';
var ad = new ActiveDirectory(config);
ad.getGroupMembershipForGroup(groupName, function(err, groups) {
if (err) {
console.log('ERROR: ' +JSON.stringify(err));
return;
}
if (! groups) console.log('Group: ' + groupName + ' not found.');
else console.log(JSON.stringify(groups));
});
Perform a generic search for the specified LDAP query filter. This function will return both groups and users that match the specified filter. Any results not recognized as a user or group (i.e. computer accounts, etc.) can be found in the 'other' attribute / array of the result.
Arguments
Example
var _ = require('underscore');
var query = 'cn=*Exchange*';
var opts = {
includeMembership : [ 'group', 'user' ], // Optionally can use 'all'
includeDeleted : false
};
var ad = new ActiveDirectory(config);
ad.find(query, function(err, results) {
if ((err) || (! results)) {
console.log('ERROR: ' + JSON.stringify(err));
return;
}
console.log('Groups');
_.each(results.groups, function(group) {
console.log(' ' + group.cn);
});
console.log('Users');
_.each(results.users, function(user) {
console.log(' ' + user.cn);
});
console.log('Other');
_.each(results.other, function(other) {
console.log(' ' + other.cn);
});
});
If tombstoning (recycle bin) is enabled for the Active Directory installation, use findDeletedObjects to retrieve items in the recycle bin.
More information about tombstoning and enabling can be found at:
Note: That when an LDAP entry / object is tombstoned, not all attributes for that item are retained. This is a limitation of Active Directory itself and not the library itself.
Arguments
If the baseDN is not specified, then a RootDSE query will be performed on the attached URL and 'ou-Deleted Objects' will be appended.
Example
var url = 'ldap://yourdomain.com';
var opts = {
baseDN: 'ou=Deleted Objects, dc=yourdomain, dc=com',
filter: 'cn=*Bob*'
};
ad.findDeletedObjects(opts, function(err, result) {
if (err) {
console.log('ERROR: ' +JSON.stringify(err));
return;
}
console.log('findDeletedObjects: '+JSON.stringify(result));
});
Looks up or finds a username by their sAMAccountName, userPrincipalName, distinguishedName (dn) or custom filter. If found, the returned object contains all of the requested attributes. By default, the following attributes are returned:
Arguments
Example
// Any of the following username types can be searched on
var sAMAccountName = 'username';
var userPrincipalName = 'username@domain.com';
var dn = 'CN=Smith\\, John,OU=Users,DC=domain,DC=com';
// Find user by a sAMAccountName
var ad = new ActiveDirectory(config);
ad.findUser(sAMAccountName, function(err, user) {
if (err) {
console.log('ERROR: ' +JSON.stringify(err));
return;
}
if (! user) console.log('User: ' + sAMAccountName + ' not found.');
else console.log(JSON.stringify(user));
});
Perform a generic search for users that match the specified filter. The default LDAP filter for users is specified as (&(|(objectClass=user)(objectClass=person))(!(objectClass=computer))(!(objectClass=group)))
Arguments
Example
var query = 'cn=*George*';
var ad = new ActiveDirectory(config);
ad.findUsers(query, true, function(err, users) {
if (err) {
console.log('ERROR: ' +JSON.stringify(err));
return;
}
if ((! users) || (users.length == 0)) console.log('No users found.');
else {
console.log('findUsers: '+JSON.stringify(users));
}
});
Looks up or find a group by common name (CN) which is required to be unique in Active Directory or optionally by the distinguished name. Supports groups with range retrieval specifiers. The following attributes are returned by default for the group:
Arguments
Example
// Any of the following group names can be searched on
var groupName = 'Employees';
var dn = 'CN=Employees,OU=Groups,DC=domain,DC=com'
// Find group by common name
var ad = new ActiveDirectory(config);
ad.findGroup(groupName, function(err, group) {
if (err) {
console.log('ERROR: ' +JSON.stringify(err));
return;
}
if (! user) console.log('Group: ' + groupName + ' not found.');
else {
console.log(group);
console.log('Members: ' + (group.member || []).length);
}
});
Perform a generic search for groups that match the specified filter. The default LDAP filter for groups is specified as (&(objectClass=group)(!(objectClass=computer))(!(objectClass=user))(!(objectClass=person)))
Arguments
Example
var query = 'CN=*Admin*';
var ad = new ActiveDirectory(config);
ad.findGroups(query, function(err, groups) {
if (err) {
console.log('ERROR: ' +JSON.stringify(err));
return;
}
if ((! groups) || (groups.length == 0)) console.log('No groups found.');
else {
console.log('findGroups: '+JSON.stringify(groups));
}
});
Retrieves the root DSE for the specified url. Can be called statically.
Arguments
Example
var url = 'ldap://yourdomain.com';
ActiveDirectory.prototype.getRootDSE(url, [ 'defaultNamingContext' ], function(err, result) {
if (err) {
console.log('ERROR: ' +JSON.stringify(err));
return;
}
console.log('getRootDSE: '+JSON.stringify(result));
});
// Or can be called with an instance...
var ad = new ActiveDirectory(config);
ad.getRootDSE(function(err, result) {
//...
});
By default, the following attributes are returned for users and groups:
If you need to override those defaults, then you can override them when you create your ActiveDirectory instance:
var ad = new ActiveDirectory({ url: 'ldap://dc.domain.com',
baseDN: 'dc=domain,dc=com',
username: 'username@domain.com',
password: 'password',
attributes: {
user: [ 'myCustomAttribute', 'mail', 'userPrinicipalName' ],
group: [ 'anotherCustomAttribute', 'objectCategory' ]
}
});
If overriding the 'user' or 'group' attribute, you must specify ALL of the attributes you want. The existing defaults will be overridden. Optionally, you can override the attributes on a per call basis using the 'opts' parameter.
By default, referral chasing is disabled. To enable it, specify a referrals attribute when you create your instance. The referrals object has the following syntax:
{
referrals: {
enabled: false,
excluded: [
'ldaps?://ForestDnsZones\./.*',
'ldaps?://DomainDnsZones\./.*',
'ldaps?://.*/CN=Configuration,.*'
]
}
}
The 'excluded' options is a list of regular expression filters to ignore specific referrals. The default exclusion list is included above, ignoring the special partitions that ActiveDirectory creates by default. To specify these options, override them as follows:
var ad = new ActiveDirectory({ url: 'ldap://dc.domain.com',
baseDN: 'dc=domain,dc=com',
username: 'username@domain.com',
password: 'password',
attributes: { ... },
referrals: {
enabled: true,
excluded: [ ]
}
});
If you enable referral chasing, the specified username MUST be a userPrincipalName.
if you want to manipulate the search entry in a different way or perhaps augment the search result with additional data, you can pass a custom parser. This is useful, for example, in case you want to change the objectSid or GUID which are binary values.
Example:
function customEntryParser(entry, raw, callback){
if (raw.hasOwnProperty("objectSid")){
entry.objectSid = raw.objectSid;
}
if (raw.hasOwnProperty("objectGUID")){
entry.objectGUID = raw.objectGUID;
}
callback(entry);
};
If you want to specify your own parser you can override the default parser as follows:
var ad = new ActiveDirectory({ url: 'ldap://dc.domain.com',
baseDN: 'dc=domain,dc=com',
username: 'username@domain.com',
password: 'password',
attributes: { ... },
referrals: { ... },
entryParser : customEntryParser
});
Optionally, you can specify your custom entry parser as part of the 'opts' object. See optional parameters for more information.
var opts = function(entry, raw, callback) {
entry.retrievedAt = new Date();
callback(entry);
};
ad.findUser(opts, 'userPrincipalName=bob@domain.com', function(err, user) {
...
});
### Optional Parameters / Extended Functionality
Any method which takes an 'opts' parameter allows for additional options. Options for both activedirectory.js and the internal ldapjs client are supported.
Currently supported ldapjs opts are:
Options for activedirectory.js:
var opts = {
scope: 'sub',
filter: 'objectClass=User',
includeMembership: [ 'user' ],
entryParser: function(entry, raw, callback) {
// returning null with exclude result
if (entry.ignore) return(null);
entry.retrievedAt = new Date();
entry.preferredServer = getPreferredServerFromDatabase(entry.userPrincipalName);
callback(entry);
}
};