How to achieve proper server side stack trace line numbers in Next.js?

Viewed 438

Our team is running fairly simple Next.js apps and are seeing incorrect line numbers in the stack traces at the console when we encounter problems in our code.

To take a trivial example, start with the create-next-app tutorial code found here. Then modify that handler function to throw an Error:

export default function handler(req, res) {
  throw new Error("line2");
  res.status(200).json({ text: 'Hello' })
}

the ./pages/api/hello.js

Then visit http://localhost:3000/api/hello

At the console we are seeing:

Error: line2
    at handler (webpack-internal:///./pages/api/hello.js:4:9)
    at apiResolver (/home/jasonnet/jack_work/nextjs-blog/node_modules/next/dist/next-server/server/api-utils.js:8:7)
    at process._tickCallback (internal/process/next_tick.js:68:7)

which lists the error being at line 4 rather than 2.

This is just a trivial example. We have found that sometimes this APPEARS to work fine (ex. on linux with Next.js 11.0), but in actually the correct line number is shown by accident and simply adding an import at the top of the file reveals that it does not actually work. So I ask...

What is the supported way to fix this in 2021 vintage Next.js (ex. v10, v11) ?

0 Answers
Related