question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@sentry/nextjs: withSentry with webpack 5 crashes server with ERR_STREAM_WRITE_AFTER_END

See original GitHub issue

Package + Version

  • @sentry/nextjs @ 6.5.0
  • next @ 10.2.3

Description

When using @sentry/nextjs with webpack 5, calling any API function wrapped in withSentry will crash the server.

Error [ERR_STREAM_WRITE_AFTER_END]: write after end
// next.config.js

const nextConfig = {
  future: {
    webpack5: true
  }
};

module.exports = withSentryConfig(nextConfig, sentryWebpackOpts);
// pages/api/hello.ts
import type { NextApiRequest, NextApiResponse } from 'next'
import { withSentry } from '@sentry/nextjs';

export default withSentry((req: NextApiRequest, res: NextApiResponse) => {
  res.status(200).json({ name: 'John Doe' })
});

After a while debugging, the withSentry function seems to throw an error (well, it throws the value false) after the API function returns. This causes Nextjs to send a 500 error response, however because the function has already sent a response, this causes an uncaught ERR_STREAM_WRITE_AFTER_END error, crashing the server.

Workarounds

  • Downgrade to @sentry/nextjs @ 6.4.1
  • Use webpack 4
  • Remove withSentry uses

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:5
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
HazATcommented, Jun 2, 2021

6.5.1 should unblock you all, we are working on a fix that resolves this.

1reaction
AbhiPrasadcommented, Jul 22, 2021

This should be fully resolved with https://github.com/getsentry/sentry-javascript/pull/3811. Please open another GH issue if there are further problems.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting ERR_STREAM_WRITE_AFTER_END after #4139
Generally, the Next.js sentry module works very well, but after 6.14.3, I experienced crashes on any request in prod:.
Read more >
Can not run server after add sentry to nextjs project
The solution to this problem is, upgrade next.js to >= 10.0.8 github.com/getsentry/sentry-javascript/issues/3724.
Read more >
Build fails after adding Sentry to a Next.js app with target
After adding Sentry to a Next.js app with target: 'serverless' using npx @sentry/wizard -i nextjs , I get the following error:
Read more >
@sentry/nextjs Webpack 5 code duplication bug - Giters
I have the same issue with @sentry/nextjs . It started happening with Next.js 10.2.1 where Sokra added some Webpack optimizations.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found