From bc73f64bf629869901dacc34a4c1185cebc8ddba Mon Sep 17 00:00:00 2001 From: Noemi <45180344+unflxw@users.noreply.github.com> Date: Fri, 26 Jul 2024 13:49:28 +0200 Subject: [PATCH] Enable source mapping in Next.js For source-mapped backtraces to be emitted for Next.js backend errors when Next.js is running in production mode, the webpack configuration needs to be tweaked in order to emit backtraces, and the `--enable-source-maps` flag needs to be passed to the Node.js runtime when starting the Next.js application, using the `NODE_OPTIONS` environment variable. --- nodejs/nextjs-13-app/app/next.config.js | 6 ++++++ nodejs/nextjs-13-app/app/package.json | 2 +- nodejs/nextjs-13-pages/app/next.config.js | 6 ++++++ nodejs/nextjs-13-pages/app/package.json | 2 +- nodejs/nextjs-14-app/app/next.config.mjs | 6 ++++++ nodejs/nextjs-14-app/app/package.json | 2 +- nodejs/nextjs-14-pages/app/next.config.mjs | 6 ++++++ nodejs/nextjs-14-pages/app/package.json | 2 +- 8 files changed, 28 insertions(+), 4 deletions(-) diff --git a/nodejs/nextjs-13-app/app/next.config.js b/nodejs/nextjs-13-app/app/next.config.js index c7aa9231..04e4460b 100644 --- a/nodejs/nextjs-13-app/app/next.config.js +++ b/nodejs/nextjs-13-app/app/next.config.js @@ -5,6 +5,12 @@ const nextConfig = { // Add the following line inside the object instrumentationHook: true, }, + webpack: (config, {isServer}) => { + if (isServer) { + config.devtool = 'eval-source-map' + } + return config + } }; module.exports = nextConfig diff --git a/nodejs/nextjs-13-app/app/package.json b/nodejs/nextjs-13-app/app/package.json index cda3f4be..e345a00e 100644 --- a/nodejs/nextjs-13-app/app/package.json +++ b/nodejs/nextjs-13-app/app/package.json @@ -5,7 +5,7 @@ "scripts": { "dev": "next dev", "build": "next build", - "start": "next start", + "start": "NODE_OPTIONS=--enable-source-maps next start", "lint": "next lint" }, "dependencies": { diff --git a/nodejs/nextjs-13-pages/app/next.config.js b/nodejs/nextjs-13-pages/app/next.config.js index 1895c372..753117a3 100644 --- a/nodejs/nextjs-13-pages/app/next.config.js +++ b/nodejs/nextjs-13-pages/app/next.config.js @@ -4,6 +4,12 @@ const nextConfig = { experimental: { instrumentationHook: true, }, + webpack: (config, {isServer}) => { + if (isServer) { + config.devtool = 'eval-source-map' + } + return config + } } module.exports = nextConfig diff --git a/nodejs/nextjs-13-pages/app/package.json b/nodejs/nextjs-13-pages/app/package.json index e2e8f927..a2a3994a 100644 --- a/nodejs/nextjs-13-pages/app/package.json +++ b/nodejs/nextjs-13-pages/app/package.json @@ -5,7 +5,7 @@ "scripts": { "dev": "next dev", "build": "next build", - "start": "next start", + "start": "NODE_OPTIONS=--enable-source-maps next start", "lint": "next lint" }, "dependencies": { diff --git a/nodejs/nextjs-14-app/app/next.config.mjs b/nodejs/nextjs-14-app/app/next.config.mjs index 4840df03..60c3e3b1 100644 --- a/nodejs/nextjs-14-app/app/next.config.mjs +++ b/nodejs/nextjs-14-app/app/next.config.mjs @@ -5,6 +5,12 @@ const nextConfig = { instrumentationHook: true, serverComponentsExternalPackages: ['@appsignal/nodejs'], }, + webpack: (config, {isServer}) => { + if (isServer) { + config.devtool = 'eval-source-map' + } + return config + } }; export default nextConfig; diff --git a/nodejs/nextjs-14-app/app/package.json b/nodejs/nextjs-14-app/app/package.json index 0ae4288f..e2524e85 100644 --- a/nodejs/nextjs-14-app/app/package.json +++ b/nodejs/nextjs-14-app/app/package.json @@ -5,7 +5,7 @@ "scripts": { "dev": "next dev", "build": "next build", - "start": "next start", + "start": "NODE_OPTIONS=--enable-source-maps next start", "lint": "next lint" }, "dependencies": { diff --git a/nodejs/nextjs-14-pages/app/next.config.mjs b/nodejs/nextjs-14-pages/app/next.config.mjs index 7653f5c3..9ee026bd 100644 --- a/nodejs/nextjs-14-pages/app/next.config.mjs +++ b/nodejs/nextjs-14-pages/app/next.config.mjs @@ -5,6 +5,12 @@ const nextConfig = { instrumentationHook: true, serverComponentsExternalPackages: ['@appsignal/nodejs'], }, + webpack: (config, {isServer}) => { + if (isServer) { + config.devtool = 'eval-source-map' + } + return config + } }; export default nextConfig; diff --git a/nodejs/nextjs-14-pages/app/package.json b/nodejs/nextjs-14-pages/app/package.json index 98b1f256..be3e6aad 100644 --- a/nodejs/nextjs-14-pages/app/package.json +++ b/nodejs/nextjs-14-pages/app/package.json @@ -5,7 +5,7 @@ "scripts": { "dev": "next dev", "build": "next build", - "start": "next start", + "start": "NODE_OPTIONS=--enable-source-maps next start", "lint": "next lint" }, "dependencies": {