Registry /
database / mongodb-client-encryption
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
ClientEncryption
✓ import { ClientEncryption } from 'mongodb-client-encryption'
✗ const { ClientEncryption } = require('mongodb-client-encryption')
While directly importable, `ClientEncryption` is typically instantiated and managed via the `mongodb` driver's `MongoClient` for auto-encryption or explicit encryption contexts.
KMSProviders
✓ import type { KMSProviders } from 'mongodb-client-encryption'
Type import for configuring Key Management System providers for `ClientEncryption`. This interface defines the shape of the `kmsProviders` object used to specify credentials for AWS, Azure, GCP, KMIP, or local KMS providers.
ClientEncryptionOptions
✓ import type { ClientEncryptionOptions } from 'mongodb-client-encryption'
Type import for the options object passed to the `ClientEncryption` constructor. This includes `keyVaultNamespace` and `kmsProviders`.
Demonstrates direct instantiation and use of `ClientEncryption` for explicit encryption and decryption of data. This showcases the core functionality provided by this library, though in most applications, client-side encryption is managed via the `mongodb` driver's `autoEncryption` settings.
import { MongoClient, Binary } from 'mongodb';
import { ClientEncryption } from 'mongodb-client-encryption';
import * as crypto from 'crypto';
async function runEncryptionExample() {
// Ensure these environment variables are set or provide default values
const connectionString = process.env.MONGO_URI ?? 'mongodb://localhost:27017/';
const kmsProviderType = process.env.KMS_PROVIDER_TYPE ?? 'local'; // e.g., 'aws', 'azure', 'gcp', 'kmip', 'local'
const keyVaultNamespace = process.env.KEY_VAULT_NAMESPACE ?? 'encryption.__keyVault';
let kmsProviders: any;
// For a 'local' KMS provider, a 32-byte master key is required.
// In a production environment, this key should be securely managed and never hardcoded.
if (kmsProviderType === 'local') {
const localMasterKey = process.env.LOCAL_MASTER_KEY || crypto.randomBytes(96).toString('base64'); // 96 bytes for CSFLE, per MongoDB documentation
kmsProviders = {
local: { key: Buffer.from(localMasterKey, 'base64').subarray(0, 32) } // Ensure it's 32 bytes for the ClientEncryptionSettings
};
} else if (kmsProviderType === 'aws') {
// Example for AWS KMS. Replace with actual credentials/configuration.
kmsProviders = {
aws: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID ?? 'YOUR_AWS_ACCESS_KEY_ID',
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY ?? 'YOUR_AWS_SECRET_ACCESS_KEY',
region: process.env.AWS_REGION ?? 'us-east-1'
}
};
} else {
console.error(`KMS Provider ${kmsProviderType} not supported in this example.`);
return;
}
const client = new MongoClient(connectionString);
try {
await client.connect();
const encryption = new ClientEncryption(client,
{
keyVaultNamespace,
kmsProviders
}
);
// IMPORTANT: In a real application, you would typically create or retrieve a Data Key ID
// from your key vault (e.g., using `encryption.createDataKey()`).
// For this quickstart, we'll use a placeholder UUID string for demonstration.
// Make sure this is a valid UUID (BSON Binary subtype 0x04) of an actual data key in your vault.
const dataKeyUuidString = process.env.DATA_KEY_UUID ?? '12345678123456781234567812345678'; // Example 16-byte hex representation for UUID
const dataKeyId = new Binary(Buffer.from(dataKeyUuidString.padEnd(32, '0'), 'hex'), Binary.SUBTYPE_UUID);
const dataToEncrypt = 'my highly sensitive information';
console.log('Original Data:', dataToEncrypt);
// Explicitly encrypt a field
const encryptedData = await encryption.encrypt(dataToEncrypt, {
keyId: dataKeyId,
algorithm: 'AEAD_AES256_CBC_HMAC_SHA512-Deterministic' // Or AEAD_AES256_CBC_HMAC_SHA512-Random
});
console.log('Encrypted Data (BSON Binary):', encryptedData);
// Explicitly decrypt the field
const decryptedData = await encryption.decrypt(encryptedData);
console.log('Decrypted Data:', decryptedData);
} finally {
await client.close();
}
}
runEncryptionExample().catch(console.error);
Errors
Common errors & fixes
Error: Node.js version 18.x.x is not supported by mongodb-client-encryption@7.x.x
Attempting to use `mongodb-client-encryption` v7 with an unsupported Node.js version.
fixUpdate Node.js to version 20.19.0 or newer (e.g., `nvm install 20.19.0 && nvm use 20.19.0`).
TypeError: ClientEncryption is not a constructor
Incorrect import or `require` statement, or `mongodb-client-encryption` failed to load its native bindings.
fixEnsure you are using `import { ClientEncryption } from 'mongodb-client-encryption';` for ESM and that the package installed correctly. This might involve checking for native module build errors during `npm install` and verifying OS/Node.js compatibility. Try `npm rebuild mongodb-client-encryption`. Error: Key Management System (KMS) provider 'local' requires a 32-byte key.
The `local` KMS provider was configured with a master key that is not exactly 32 bytes long.
fixEnsure the key provided to the `local` KMS provider is a 32-byte `Buffer`. For example, `key: Buffer.from('YOUR_32_BYTE_LOCAL_KEY_HERE').subarray(0, 32)` or generate securely. MongoDriverError: Queryable Encryption is not supported on MongoDB Community Edition for automatic encryption.
Attempting to use automatic encryption features with MongoDB Community Edition or a standalone instance, which is only supported by MongoDB Enterprise or Atlas. Explicit encryption, however, works.
fixUse MongoDB Enterprise, MongoDB Atlas, or switch to explicit encryption operations with `ClientEncryption` for MongoDB Community.
Audit
Dependencies
mongodbrequiredThis package is an internal dependency of the `mongodb` driver and its public API is primarily exposed through the driver. It requires a compatible major version of the `mongodb` driver.