Skip to content

TypeError: Cannot read properties of undefined (reading '0') in getMetaLines #5

@domdomegg

Description

@domdomegg

Description

The @epegzz/winston-dev-console package throws a TypeError when used with Winston logger in certain configurations. The error occurs in the getMetaLines function where it attempts to access info[SPLAT][0] but info[SPLAT] is undefined.

Error Message

TypeError: Cannot read properties of undefined (reading '0')
    at DevConsoleFormat.getMetaLines (/path/to/node_modules/@epegzz/winston-dev-console/dist/format.js:82:66)
    at DevConsoleFormat.transform (/path/to/node_modules/@epegzz/winston-dev-console/dist/format.js:144:21)
    at Format.transform (/path/to/node_modules/logform/combine.js:20:24)
    at Console._write (/path/to/node_modules/winston-transport/modern.js:91:33)
    at doWrite (/path/to/node_modules/readable-stream/lib/_stream_writable.js:390:139)
    at writeOrBuffer (/path/to/node_modules/readable-stream/lib/_stream_writable.js:381:5)
    at Writable.write (/path/to/node_modules/readable-stream/lib/_stream_writable.js:302:11)
    at DerivedLogger.ondata (/path/to/node_modules/readable-stream/lib/_stream_readable.js:629:20)
    at DerivedLogger.emit (node:events:507:28)
    at addChunk (/path/to/node_modules/readable-stream/lib/_stream_readable.js:279:12)

Environment

  • Node.js: v23.11.0
  • winston: 3.17.0
  • @epegzz/winston-dev-console: 1.3.4
  • OS: macOS

Steps to Reproduce

  1. Create a Winston logger with the following configuration:
import winston from 'winston';
import winstonDevConsole from '@epegzz/winston-dev-console';

const logger = winston.createLogger({
  level: 'info',
  format: winston.format.combine(
    winston.format.timestamp(),
    winston.format.errors({ stack: true }),
  ),
  transports: [
    new winston.transports.Console({
        format: winstonDevConsole.default.format(),
    }),
  ],
});

logger.info('Simple message');
  1. Run the code
  2. Observe the TypeError

Expected Behavior

The logger should format and display the message without throwing an error.

Actual Behavior

The application crashes with a TypeError when trying to access the undefined SPLAT property.

Root Cause Analysis

The issue appears to be in /dist/format.js at line 82:

const splat = Object.assign({}, info[triple_beam_1.SPLAT][0]);

The code assumes that info[triple_beam_1.SPLAT] exists and is an array, but this property is not always present depending on how the logger is configured and used. The code should check if the SPLAT property exists before trying to access its first element.

Suggested Fix

Add a null/undefined check before accessing the SPLAT property:

const splat = info[triple_beam_1.SPLAT] && info[triple_beam_1.SPLAT][0] 
  ? Object.assign({}, info[triple_beam_1.SPLAT][0]) 
  : {};

Minimal Reproducible Example

A complete MRE is available at: https://github.com/domdomegg/winston-dev-console-5-mre

Logs here: https://github.com/domdomegg/winston-dev-console-5-mre/actions/runs/15639287995/job/44062462760

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions