{"record":{"id":"507e4f23d48de341","repo":"winstonjs/winston","slug":"cannot-set-name-and-target-together","errorCode":null,"errorMessage":"Cannot set ${name} and ${target} together","messagePattern":"Cannot set (.+?) and (.+?) together","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/winston/transports/file.js","lineNumber":44,"sourceCode":" */\nmodule.exports = class File extends TransportStream {\n  /**\n   * Constructor function for the File transport object responsible for\n   * persisting log messages and metadata to one or more files.\n   * @param {Object} options - Options for this instance.\n   */\n  constructor(options = {}) {\n    super(options);\n\n    // Expose the name of this Transport on the prototype.\n    this.name = options.name || 'file';\n\n    // Helper function which throws an `Error` in the event that any of the\n    // rest of the arguments is present in `options`.\n    function throwIf(target, ...args) {\n      args.slice(1).forEach(name => {\n        if (options[name]) {\n          throw new Error(`Cannot set ${name} and ${target} together`);\n        }\n      });\n    }\n\n    // Setup the base stream that always gets piped to to handle buffering.\n    this._stream = new PassThrough();\n    this._stream.setMaxListeners(30);\n\n    // Bind this context for listener methods.\n    this._onError = this._onError.bind(this);\n\n    if (options.filename || options.dirname) {\n      throwIf('filename or dirname', 'stream');\n      this._basename = this.filename = options.filename\n        ? path.basename(options.filename)\n        : 'winston.log';\n\n      this.dirname = options.dirname || path.dirname(options.filename);","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/winstonjs/winston/blob/ff0b79de8562bb322c390fbc82fe71c11f373428/lib/winston/transports/file.js#L26-L62","documentation":"The File transport constructor defines throwIf(target, ...names) which throws when mutually exclusive options are set together: the message lists which two options conflict. In winston v3 a File transport must log either to a stream or to a file (filename/maxsize) — never both — because the internal pipeline is built one way or the other.","triggerScenarios":"new winston.transports.File({ stream: fs.createWriteStream('a.log'), filename: 'b.log' }) or { stream, maxsize } — the constructor calls throwIf('stream', 'filename', 'maxsize') and throwIf('filename', 'stream').","commonSituations":"Merging config objects where defaults include filename and the caller also injects a stream; copying an existing transport's options and adding a stream for rotation handling; migrating code that piped streams manually on top of filename-based logging.","solutions":["Pick one target: remove the stream option and keep filename (most common), or remove filename/maxsize and keep stream.","If config is merged, explicitly delete the conflicting key before constructing: delete opts.stream when filename is set.","If you need both streamed writes and rotation, pipe your own stream into the transport's stream instead of passing both options."],"exampleFix":"// before\nnew winston.transports.File({ filename: 'app.log', stream: fs.createWriteStream('app.log') });\n// after\nnew winston.transports.File({ filename: 'app.log' });","handlingStrategy":"validation","validationCode":"function makeFileTransport(opts) {\n  if (opts.stream && (opts.filename || opts.maxsize)) {\n    throw new TypeError('File transport: stream is mutually exclusive with filename/maxsize');\n  }\n  return new winston.transports.File(opts);\n}","typeGuard":null,"tryCatchPattern":"try {\n  fileTransport = new winston.transports.File(mergedOpts);\n} catch (err) {\n  if (err.message.startsWith('Cannot set')) {\n    const { stream, ...rest } = mergedOpts; // prefer filename\n    fileTransport = new winston.transports.File(rest);\n  } else throw err;\n}","preventionTips":["Never merge stream and filename in one options object; strip one key explicitly after merging configs.","Prefer filename/maxsize for normal file logging; reserve stream for piping custom write streams.","Add a startup assertion that logs which conflicting keys were provided."],"tags":["winston","file-transport","conflicting-options","constructor-validation"],"backgroundTag":"conflicting-options","analyzedSha":"ff0b79de8562bb322c390fbc82fe71c11f373428","analyzedAt":"2026-08-31T13:33:44.585Z","schemaVersion":2},"datasetVersion":"2026-09-01T08:17:40.651Z"}