{"id":"a8a2e3c5695dc44d","repo":"sidorares/node-mysql2","slug":"user-connection-config-property-must-be-a-string-a8a2e3","errorCode":null,"errorMessage":"\"user\" connection config property must be a string","messagePattern":"\"user\" connection config property must be a string","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/packets/handshake_response.js","lineNumber":126,"sourceCode":"          connectAttributes[attrNames[k]],\n          encoding\n        );\n      }\n      packet.writeLengthCodedNumber(keysLength);\n      for (k = 0; k < attrNames.length; ++k) {\n        packet.writeLengthCodedString(attrNames[k], encoding);\n        packet.writeLengthCodedString(\n          connectAttributes[attrNames[k]],\n          encoding\n        );\n      }\n    }\n    return packet;\n  }\n\n  toPacket() {\n    if (typeof this.user !== 'string') {\n      throw new Error('\"user\" connection config property must be a string');\n    }\n    if (typeof this.database !== 'string') {\n      throw new Error('\"database\" connection config property must be a string');\n    }\n    // dry run: calculate resulting packet length\n    const p = this.serializeResponse(Packet.MockBuffer());\n    return this.serializeResponse(Buffer.alloc(p.offset));\n  }\n  static fromPacket(packet, serverFlags = 0xffffffff) {\n    const args = {};\n    args.clientFlags = packet.readInt32();\n    function isSet(flag) {\n      return args.clientFlags & serverFlags & ClientConstants[flag];\n    }\n    args.maxPacketSize = packet.readInt32();\n    args.charsetNumber = packet.readInt8();\n    const encoding = CharsetToEncoding[args.charsetNumber];\n    args.encoding = encoding;","sourceCodeStart":108,"sourceCodeEnd":144,"githubUrl":"https://github.com/sidorares/node-mysql2/blob/5ebe8903d6aea2d8ea1490e11b52491526e50f19/lib/packets/handshake_response.js#L108-L144","documentation":"Thrown by HandshakeResponse.toPacket() when serializing the MySQL handshake response packet. The library requires that the 'user' connection-config property be a string because it is written into the authentication packet via writeLengthCodedString. If user is undefined, a number, or any non-string type, the check at handshake_response.js:125 rejects it before any network I/O. This is a fail-fast guard against malformed config reaching the wire.","triggerScenarios":"Calling createConnection({ user: undefined }), createConnection({ user: 123 }), or omitting the user field entirely (so it defaults to undefined). Also triggered if a config object is built dynamically where user ends up null/number/object, or when env vars like process.env.DB_USER are unset and assigned to user.","commonSituations":"Loading credentials from environment variables that are not set (undefined), parsing JSON config where the user key is missing, passing a numeric user id from an internal system instead of the username string, or accidentally passing a pool/cluster config that lacks user.","solutions":["Ensure the user property is a string in the connection config: { user: 'root' }","Validate config.user before creating the connection, e.g. if (typeof config.user !== 'string') throw ...","Provide a fallback from environment: { user: process.env.DB_USER || 'root' }","Check that environment variables DB_USER / MYSQL_USER are exported in the current shell or .env file"],"exampleFix":"// before\nconst conn = mysql.createConnection({ host: 'localhost' }); // user missing\n\n// after\nconst conn = mysql.createConnection({\n  host: 'localhost',\n  user: process.env.DB_USER || 'root',\n  password: process.env.DB_PASSWORD,\n});","handlingStrategy":"validation","validationCode":"function assertUserConfig(config) {\n  if (typeof config.user !== 'string' || config.user.length === 0) {\n    throw new TypeError('mysql2: config.user must be a non-empty string');\n  }\n}\n// call before createConnection / createPool\nassertUserConfig(config);","typeGuard":"function isUserConfig(c) {\n  return c != null && typeof c.user === 'string';\n}","tryCatchPattern":null,"preventionTips":["Centralize DB config in one module that validates required string fields (user, host) at startup","Use a schema validator (zod, joi) on the config object before passing to mysql2","Always default user from env with a string fallback: process.env.DB_USER || 'root'","Fail fast at boot: if (typeof config.user !== 'string') throw, so the error points at config, not the handshake"],"tags":["config","authentication","connection","validation"],"analyzedSha":"5ebe8903d6aea2d8ea1490e11b52491526e50f19","analyzedAt":"2026-08-03T18:58:53.602Z","schemaVersion":2}