ldap-filter
raw JSON → 0.3.3 verified Sat Apr 25 auth: no javascript
API for handling LDAP-style filters, originally derived from filter code in LDAPjs. Version 0.3.3 is the latest stable release. This package provides utilities to parse, serialize, and manipulate LDAP filter strings (e.g., RFC 4515). It is minimal and focused, suitable for server-side Node.js applications that need to handle LDAP filter syntax without the full LDAPjs library. Release cadence is low; no major updates since 2019.
Common errors
error Cannot find module 'ldap-filter' ↓
cause Package not installed or import path incorrect.
fix
npm install ldap-filter and use import { parseString } from 'ldap-filter'
error TypeError: ldap_filter_1.parseString is not a function ↓
cause Using require('ldap-filter') which returns a module with default export instead of named exports.
fix
Use import { parseString } from 'ldap-filter' or const { parseString } = await import('ldap-filter')
error SyntaxError: Unexpected token 'export' ↓
cause Project is not configured for ESM (e.g., CommonJS only).
fix
Add 'type': 'module' to package.json or use dynamic import().
Warnings
breaking Version 0.3.x drops support for Node.js < 0.8 ↓
fix Upgrade Node.js to >=0.8 or stick with older version 0.2.x
deprecated The parseString function may be deprecated in future versions in favor of a class method. ↓
fix Check future releases for migration guides; currently parseString still works.
gotcha Filter values are not escaped by default; manual escaping may be needed for special characters. ↓
fix Use `filter.attribute` and `filter.value` directly; consider encoding with encodeURIComponent if required.
gotcha The package does not validate filter string syntax; invalid filters may return unexpected objects. ↓
fix Validate input before parsing; use try-catch to handle potential parse errors.
Install
npm install ldap-filter yarn add ldap-filter pnpm add ldap-filter Imports
- parseString wrong
const { parseString } = require('ldap-filter')correctimport { parseString } from 'ldap-filter' - Filter wrong
const Filter = require('ldap-filter').Filtercorrectimport { Filter } from 'ldap-filter' - EqualityFilter wrong
const EqualityFilter = require('ldap-filter').EqualityFiltercorrectimport { EqualityFilter } from 'ldap-filter'
Quickstart
import { parseString, EqualityFilter } from 'ldap-filter';
// Parse an LDAP filter string
const filter = parseString('(cn=John Doe)');
console.log(filter.constructor.name); // EqualityFilter
console.log(filter.attribute); // 'cn'
console.log(filter.value); // 'John Doe'
console.log(filter.toString()); // '(cn=John Doe)'
// Or build a filter programmatically
const eqFilter = new EqualityFilter({
attribute: 'uid',
value: 'jdoe'
});
console.log(eqFilter.toString()); // '(uid=jdoe)'
// Chain filters with boolean combinators
import { AndFilter, OrFilter } from 'ldap-filter';
const andFilter = new AndFilter({
filters: [
new EqualityFilter({ attribute: 'sn', value: 'Doe' }),
new EqualityFilter({ attribute: 'givenName', value: 'John' })
]
});
console.log(andFilter.toString()); // '(&(sn=Doe)(givenName=John))'