mongodb/node-mongodb-native · error · MongoParseError

Cannot have empty URI params in DNS TXT Record

Error message

Cannot have empty URI params in DNS TXT Record

What it means

Thrown by resolveSRVRecord() when a TXT record (for a mongodb+srv:// URI) declares an allowed key but with an empty value, e.g. 'authSource='. The driver parses the TXT record as URLSearchParams and rejects any of authSource/replicaSet/loadBalanced whose value is the empty string. It is a MongoParseError.

Source

Thrown at src/connection_string.ts:127

  } catch (error) {
    if (error.code !== 'ENODATA' && error.code !== 'ENOTFOUND') {
      throw error;
    }
    return hostAddresses;
  }

  if (record.length > 1) {
    throw new MongoParseError('Multiple text records not allowed');
  }

  const txtRecordOptions = new URLSearchParams(record[0].join(''));
  const txtRecordOptionKeys = [...txtRecordOptions.keys()];
  if (txtRecordOptionKeys.some(key => !VALID_TXT_RECORDS.includes(key))) {
    throw new MongoParseError(`Text record may only set any of: ${VALID_TXT_RECORDS.join(', ')}`);
  }

  if (VALID_TXT_RECORDS.some(option => txtRecordOptions.get(option) === '')) {
    throw new MongoParseError('Cannot have empty URI params in DNS TXT Record');
  }

  const source = txtRecordOptions.get('authSource') ?? undefined;
  const replicaSet = txtRecordOptions.get('replicaSet') ?? undefined;
  const loadBalanced = txtRecordOptions.get('loadBalanced') ?? undefined;

  if (
    !options.userSpecifiedAuthSource &&
    source &&
    options.credentials &&
    !AUTH_MECHS_AUTH_SRC_EXTERNAL.has(options.credentials.mechanism)
  ) {
    options.credentials = MongoCredentials.merge(options.credentials, { source });
  }

  if (!options.userSpecifiedReplicaSet && replicaSet) {
    options.replicaSet = replicaSet;
  }

View on GitHub (pinned to 3366c21a63)

Solutions

  1. Inspect the TXT record: dig <host> TXT and check for trailing '='.
  2. Remove keys that have no value, or supply the intended value.
  3. Re-query DNS after the change and confirm each key has a non-empty value.
  4. Validate the TXT record locally with new URLSearchParams(record) before publishing.

Example fix

// DNS TXT before: "authSource=&replicaSet=rs0"
// DNS TXT after:  "replicaSet=rs0"
Defensive patterns

Strategy: validation

Validate before calling

import { promises as dns } from 'dns';
const ALLOWED = ['authSource', 'replicaSet', 'loadBalanced'];
async function validateTxtValues(host: string): Promise<void> {
  const records = await dns.resolveTxt(host);
  if (!records.length) return;
  const params = new URLSearchParams(records[0].join(''));
  for (const k of ALLOWED) {
    if (params.has(k) && (params.get(k) ?? '') === '') {
      throw new Error(`TXT key '${k}' must not have an empty value`);
    }
  }
}

Try / catch

try {
  await client.connect();
} catch (e) {
  if (e instanceof MongoParseError && /empty URI params in DNS TXT/.test(e.message)) {
    throw new Error('DNS TXT record has an empty value; fix or remove the key');
  }
  throw e;
}

Prevention

When it happens

Trigger: A TXT record formatted as 'authSource=&replicaSet=rs0' (trailing equals with nothing); a copy-paste error leaving a value blank; a DNS templating tool that emitted empty values for unset variables.

Common situations: Editing the TXT record and deleting a value but leaving the key; using a templating system that interpolates missing vars as empty strings; partial migration where one option was intended to be removed but the key was left.

Related errors


AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04). Data as JSON: /data/errors/1b9c80cce3f99981.json. Report an issue: GitHub.