{"id":695,"hash":"39e175884bc33dede9e7d610e41e1a4b5a67e6f0647733400c33e51307c31a5a","pattern":"ERR_HTTP_HEADERS_SENT: Cannot set headers after they are sent to the client","full_message":"I'm facing this weird issue in NodeJS when using with Passport.js, Express and Mongoose. Basically, I get an error saying \"Cannot set headers after they are sent to the client\" even though I don't send more than one header.\n\nI've read other posts and tried them out as well, and none of them worked.\n\napp.get - is there any difference between res.send vs return res.send\nError [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client\nCannot set headers after they are sent to the client\n\nI've dug through github issues and I can't seem to find a solution. I get the problem that this error is triggered when I send multiple response headers, but the fact is that I am not sending multiple headers. It seems just weird.\n\nThis is my stack trace:\n\n  (node:9236) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.\n\n  \n  Server Running on port 5000\n\n  MongoDB Connected Error\n\n  [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the\n  client\n\n    at validateHeader (_http_outgoing.js:503:11)\n\n     at ServerResponse.setHeader (_http_outgoing.js:510:3)\n\n     at ServerResponse.header (/Users/lourdesroashan/code/github/devlog/node_modules/express/lib/response.js:767:10)\n\n     at ServerResponse.json (/Users/lourdesroashan/code/github/devlog/node_modules/express/lib/response.js:264:10)\n\n     at Profile.findOne.then.profile (/Users/lourdesroashan/code/github/devlog/routes/api/profile.js:27:30)\n\n     at <anonymous>\n\nThis is my server code:\n\nrouter.get(\"/userprofile\", passport.authenticate('jwt', { session: false }), (req, res) => {\n\n  Profile.findOne({ user: req.user.id }).then(profile => {\n    if (!profile) {\n      return res.status(404).json({ error: \"No Profile Found\" });\n    }\n    else {\n      res.json(profile);\n    }\n  }).catch(err => {\n    console.log(err);\n  })\n});\n\nI understand what the error means, but from what I know, I don't think I am sending multiple headers, I even checked by console.log that only one of the blocks is run.\n\nThank you so much in advance! :)\n\nFull Code at: https://github.com/lourdesr/devlog\n\nEDIT:\n\nI figured it out. It was a problem in my passport.js while trying to get the authenticated user. I forgot to use 'return' on the 'done' method, which had caused it. Just added the return statement and it worked!","ecosystem":"npm","package_name":"node.js","package_version":null,"solution":"That particular error occurs whenever your code attempts to send more than one response to the same request.  There are a number of different coding mistakes that can lead to this:\n\nImproperly written asynchronous code that allows multiple branches to send a response.\nNot returning from the request handler to stop further code in the request handler from running after you've sent a response.\nCalling next() when you've already sent a response.\nImproper logic branching that allows multiple code paths to execute and attempt to send a response.\n\nThe code you show in your question does not appear like it would cause that error, but I do see code in a different route that would cause that error.\n\nWhere you have this:\n\nif (!user) {\n  errors.email = \"User not found\";\n  res.status(404).json({ errors });\n}\n\nYou need to change it to:\n\nif (!user) {\n  errors.email = \"User not found\";\n  res.status(404).json({ errors });\n  // stop further execution in this callback\n  return;\n}\n\nYou don't want the code to continue after you've done res.status(404).json({ errors }); because it will then try to send another response.\n\nIn addition, everywhere you have this:\n\nif (err) throw err;\n\ninside an async callback, you need to replace that with something that actually sends an error response such as:\n\nif (err) {\n    console.log(err);\n    res.sendStatus(500);\n    return;\n}\n\nthrowing inside an async callback just goes back into the node.js event system and isn't thrown to anywhere that you can actually catch it.  Further, it doesn't send a response to the http request.  In other words, it doesn't really do what the server is supposed to do.  So, do yourself a favor and never write that code in your server.  When you get an error, send an error response.\n\nOr, you can send to centralized Express error handling:\n\nif (err) {\n    console.log(err);\n    next(err);\n    return;\n}\n\nAnd, then you can catch all of these in an Express error handler.\n\nSince it looks like you may be new here, I wanted to compliment you on including a link to your full source code at https://github.com/lourdesr/devlog because it's only by looking at that that I was able to see this place where the error is occuring.","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/52122272/err-http-headers-sent-cannot-set-headers-after-they-are-sent-to-the-client","votes":138,"created_at":"2026-04-19T04:51:30.404870+00:00","updated_at":"2026-04-19T04:51:30.404870+00:00"}