mongodb/node-mongodb-native · error · MongoAPIError

Option 'mongodbLogComponentSeverities' must be a non-null…

Error message

Option 'mongodbLogComponentSeverities' must be a non-null object

What it means

`mongodbLogComponentSeverities` maps log components to severity levels. The transform at src/connection_string.ts:1255 requires the value to be a non-null object (`typeof value === 'object' && value`). Passing `null`, a string, a number, an array, or `undefined`-as-other-types triggers MongoAPIError at src/connection_string.ts:1257.

Solutions

  1. Pass an object keyed by component: `mongodbLogComponentSeverities: { default: 'info' }`.
  2. Use the dedicated key `default` to set a catch-all severity.
  3. Leave the option unset to rely on environment-derived defaults.

Example fix

// before
new MongoClient(uri, { mongodbLogComponentSeverities: 'debug' });

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

Strategy: type-guard

Validate before calling

if (opts.mongodbLogComponentSeverities != null && (typeof opts.mongodbLogComponentSeverities !== 'object' || opts.mongodbLogComponentSeverities === null || Array.isArray(opts.mongodbLogComponentSeverities))) {
  throw new Error('mongodbLogComponentSeverities must be a non-null object');
}

Type guard

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

Try / catch

try { client = new MongoClient(uri, opts); } catch (e) { if (e instanceof MongoAPIError && /must be a non-null object/.test(e.message)) { opts.mongodbLogComponentSeverities = { default: 'info' }; client = new MongoClient(uri, opts); } else throw e; }

Prevention

When it happens

Trigger: Passing `mongodbLogComponentSeverities: 'debug'` (string); `mongodbLogComponentSeverities: null`; `mongodbLogComponentSeverities: 3` (number); or `mongodbLogComponentSeverities: ['command']` (array).

Common situations: Confusing with `mongodbLogPath`/`mongodbLogComponentSeverities` env-var style which takes single strings; providing a single global level instead of a per-component map; JSON config that decodes `null` for missing fields.

Related errors


AI-assisted analysis of mongodb/node-mongodb-native@dce7939f86 (2026-08-11). Data as JSON: /api/errors/f76cd27c313b6475. Report an issue: GitHub.

Appendix: 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 dce7939f86)