{"id":"2882e8ccfc1b2baa","repo":"fastify/fastify","slug":"fst-err-ctp-already-present","errorCode":"FST_ERR_CTP_ALREADY_PRESENT","errorMessage":"Content type parser '%s' already present.","messagePattern":"Content type parser '(.+?)' already present\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/content-type-parser.js","lineNumber":59,"sourceCode":"  this.cache = new Fifo(100)\n}\n\nContentTypeParser.prototype.add = function (contentType, opts, parserFn) {\n  const contentTypeIsString = typeof contentType === 'string'\n\n  if (contentTypeIsString) {\n    contentType = contentType.trim().toLowerCase()\n    if (contentType.length === 0) throw new FST_ERR_CTP_EMPTY_TYPE()\n  } else if (!(contentType instanceof RegExp)) {\n    throw new FST_ERR_CTP_INVALID_TYPE()\n  }\n\n  if (typeof parserFn !== 'function') {\n    throw new FST_ERR_CTP_INVALID_HANDLER()\n  }\n\n  if (this.existingParser(contentType)) {\n    throw new FST_ERR_CTP_ALREADY_PRESENT(contentType)\n  }\n\n  if (opts.parseAs !== undefined) {\n    if (opts.parseAs !== 'string' && opts.parseAs !== 'buffer') {\n      throw new FST_ERR_CTP_INVALID_PARSE_TYPE(opts.parseAs)\n    }\n  }\n\n  const parser = new Parser(\n    opts.parseAs === 'string',\n    opts.parseAs === 'buffer',\n    opts.bodyLimit,\n    parserFn\n  )\n\n  if (contentType === '*') {\n    this.customParsers.set('', parser)\n  } else {","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/fastify/fastify/blob/7299a57d3fdce7da49f2c6d19ba08dc673e53f22/lib/content-type-parser.js#L41-L77","documentation":"Thrown by ContentTypeParser.prototype.add (content-type-parser.js:58-59) when a parser is already registered for the given content type. existingParser() returns true if a custom parser was set, or if the type is one of the built-ins ('application/json', 'text/plain') that already have default parsers. The message includes the offending content type via %s.","triggerScenarios":"Calling `addContentTypeParser('application/json', ...)` a second time, or registering a parser for 'text/plain' when only the default exists (the default counts as present for this check unless you are replacing the default JSON/text parser — note existingParser special-cases the defaults to allow replacing them once).","commonSituations":"Two plugins both registering a parser for the same media type; re-registering after a hot-reload without a fresh instance; registering for 'application/json' forgetting Fastify already parses JSON.","solutions":["Use a different content type, or remove the existing parser first with removeContentTypeParser before re-adding.","To replace the default JSON/text parser, call addContentTypeParser once for that type (the defaults are replaceable once).","Check with `fastify.hasContentTypeParser(type)` before registering to avoid collisions."],"exampleFix":"// before\napp.addContentTypeParser('application/json', { parseAs: 'string' }, myParser)\napp.addContentTypeParser('application/json', { parseAs: 'string' }, other) // throws\n\n// after\napp.removeContentTypeParser('application/json')\napp.addContentTypeParser('application/json', { parseAs: 'string' }, other)","handlingStrategy":"validation","validationCode":"// Check existence before registering to avoid collisions.\nfunction addParserIfMissing(app, type, opts, fn) {\n  if (app.hasContentTypeParser(type)) {\n    // For defaults, remove first; for custom, skip or replace deliberately\n    app.removeContentTypeParser(type)\n  }\n  return app.addContentTypeParser(type, opts, fn)\n}","typeGuard":null,"tryCatchPattern":"try {\n  app.addContentTypeParser(type, opts, fn)\n} catch (err) {\n  if (err.code === 'FST_ERR_CTP_ALREADY_PRESENT') {\n    app.log.warn({ type }, 'parser already registered; skipping')\n  } else {\n    throw err\n  }\n}","preventionTips":["Call hasContentTypeParser(type) before registering to detect collisions.","Use removeContentTypeParser before re-registering a type.","Coordinate parser registration across plugins to avoid duplicate keys."],"tags":["content-type-parser","configuration","duplicate","fastify"],"analyzedSha":"7299a57d3fdce7da49f2c6d19ba08dc673e53f22","analyzedAt":"2026-08-03T17:43:01.300Z","schemaVersion":2}