mongodb/node-mongodb-native · error · MongoAPIError

Option 'mongodbLogPath' must be of type 'stderr' | 'stdout'

Error message

Option 'mongodbLogPath' must be of type 'stderr' | 'stdout' | MongoDBLogWritable

What it means

Thrown by the mongodbLogPath option transform when the supplied value is neither the string 'stderr', the string 'stdout', nor an object implementing a write function (MongoDBLogWritable). The driver's structured logger needs a known sink to route log output to.

Source

Thrown at src/connection_string.ts:1247

      throw new MongoParseError(`Cannot make WriteConcern from wtimeout`);
    }
  },
  zlibCompressionLevel: {
    default: 0,
    type: 'int'
  },
  mongodbLogPath: {
    transform({ values: [value] }) {
      if (
        !(
          (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') {

View on GitHub (pinned to 3366c21a63)

Solutions

  1. Use one of the literal strings: { mongodbLogPath: 'stderr' } or { mongodbLogPath: 'stdout' }.
  2. Provide a writable object: { mongodbLogPath: { write: (chunk) => myStream.write(chunk) } }.
  3. If you need a file, wrap fs.createWriteStream('./mongo.log') and pass the stream object itself (it has a write method).

Example fix

// before
new MongoClient(uri, { mongodbLogPath: '/var/log/mongo.log' });
// after
const fs = require('fs');
new MongoClient(uri, { mongodbLogPath: fs.createWriteStream('/var/log/mongo.log') });
Defensive patterns

Strategy: type-guard

Validate before calling

function isValidLogPath(v) {
  return v === 'stderr' || v === 'stdout' || (v != null && typeof v === 'object' && typeof v.write === 'function');
}

Type guard

function isMongoDBLogWritable(v): v is 'stderr' | 'stdout' | { write: (chunk: any) => void } {
  return v === 'stderr' || v === 'stdout' || (!!v && typeof v === 'object' && typeof (v as any).write === 'function');
}

Prevention

When it happens

Trigger: Setting mongodbLogPath to a file path string (e.g. '/var/log/mongo.log'), a number, a boolean, a Promise, or an object without a write method. Passing a Node.js fs.WriteStream directly also fails because the check requires the object itself to have a write function (WriteStream does, but a wrapper or path string does not).

Common situations: Assuming mongodbLogPath accepts a filesystem path like typical logging libraries; passing a Winston/pino transport object whose interface differs; enabling driver logging (mongodbLogPath is part of the newer logging API) without reading the writable contract.

Related errors


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