mongodb/node-mongodb-native · error · MongoAPIError

Option 'mongodbLogComponentSeverities' must be a non-null ob

Error message

Option 'mongodbLogComponentSeverities' must be a non-null object

What it means

Thrown by the mongodbLogComponentSeverities transform when the value is not an object or is null. This option configures per-component log severity and must be a map of component-name to severity-level strings.

Source

Thrown at src/connection_string.ts:1257

        !(
          (typeof value === 'string' && ['stderr', 'stdout'].includes(value)) ||
          (value &&
            typeof value === 'object' &&
            'write' in value &&
            typeof value.write === 'function')
        )
      ) {
        throw new MongoAPIError(
          `Option 'mongodbLogPath' must be of type 'stderr' | 'stdout' | MongoDBLogWritable`
        );
      }
      return value;
    }
  },
  mongodbLogComponentSeverities: {
    transform({ values: [value] }) {
      if (typeof value !== 'object' || !value) {
        throw new MongoAPIError(`Option 'mongodbLogComponentSeverities' must be a non-null object`);
      }
      for (const [k, v] of Object.entries(value)) {
        if (typeof v !== 'string' || typeof k !== 'string') {
          throw new MongoAPIError(
            `User input for option 'mongodbLogComponentSeverities' object cannot include a non-string key or value`
          );
        }
        if (!Object.values(MongoLoggableComponent).some(val => val === k) && k !== 'default') {
          throw new MongoAPIError(
            `User input for option 'mongodbLogComponentSeverities' contains invalid key: ${k}`
          );
        }
        if (!Object.values(SeverityLevel).some(val => val === v)) {
          throw new MongoAPIError(
            `Option 'mongodbLogComponentSeverities' does not support ${v} as a value for ${k}`
          );
        }
      }

View on GitHub (pinned to 3366c21a63)

Solutions

  1. Pass an object: { mongodbLogComponentSeverities: { command: 'debug', default: 'info' } }.
  2. If you only want a global level, use { default: 'debug' }.
  3. Remove the option entirely if you did not intend per-component severities.

Example fix

// before
new MongoClient(uri, { mongodbLogComponentSeverities: 'debug' });
// after
new MongoClient(uri, { mongodbLogComponentSeverities: { default: 'debug' } });
Defensive patterns

Strategy: type-guard

Validate before calling

function isValidComponentSeverities(v) {
  return v != null && typeof v === 'object' && !Array.isArray(v);
}

Type guard

function isComponentSeverities(v): v is Record<string, string> {
  return v != null && typeof v === 'object' && !Array.isArray(v);
}

Prevention

When it happens

Trigger: Passing mongodbLogComponentSeverities as a string (e.g. 'command=debug'), an array, a number, null, or undefined-but-coerced. The guard `typeof value !== 'object' || !value` rejects primitives and null.

Common situations: Confusing this option's object format with a typical log-level string env var like LOG_LEVEL=debug; passing a JSON-parsed null; copying config from a library that uses a different severity schema.

Related errors


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