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

`mongodbLogPath` selects where the driver's structured logger writes. The transform at src/connection_string.ts:1237 requires the value to be either the string `'stderr'`, the string `'stdout'`, or a non-null object exposing a `write` function (MongoDBLogWritable). Anything else — including a file path string — throws MongoAPIError at src/connection_string.ts:1247.

Solutions

  1. Use `'stderr'` or `'stdout'` for console output.
  2. For a file, create a writable stream and pass it: `mongodbLogPath: fs.createWriteStream('/var/log/mongo.log')`.
  3. Ensure the object you pass actually has a `write` method (e.g. a Node stream.Writable, not a string path).

Example fix

// before
new MongoClient(uri, { mongodbLogPath: '/var/log/mongo.log' });

// after
import fs from 'fs';
new MongoClient(uri, { mongodbLogPath: fs.createWriteStream('/var/log/mongo.log') });
Defensive patterns

Strategy: type-guard

Validate before calling

function isLogPath(v: unknown): boolean {
  return v === 'stderr' || v === 'stdout' || (!!v && typeof v === 'object' && typeof (v as any).write === 'function');
}
if (opts.mongodbLogPath != null && !isLogPath(opts.mongodbLogPath)) {
  throw new Error("mongodbLogPath must be 'stderr' | 'stdout' | MongoDBLogWritable");
}

Type guard

function isMongoDBLogWritable(v: unknown): v is { write: (...a: any[]) => any } { return !!v && typeof v === 'object' && typeof (v as any).write === 'function'; }

Try / catch

try { client = new MongoClient(uri, opts); } catch (e) { if (e instanceof MongoAPIError && /mongodbLogPath/.test(e.message)) { opts.mongodbLogPath = 'stderr'; client = new MongoClient(uri, opts); } else throw e; }

Prevention

When it happens

Trigger: Passing `mongodbLogPath: '/var/log/mongo.log'` (treating it as a file path); `mongodbLogPath: true`; `mongodbLogPath: 2` (a numeric fd).

Common situations: Assuming file-path semantics like pino/winston; copy-pasting a path from server logging docs; wanting to log to a file without realizing a writable stream is required.

Related errors


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

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