sentry-javascript
sentry-javascript copied to clipboard
Mixing node and edge instrumentations causes webpack errors
Is there an existing issue for this?
- [X] I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues
- [X] I have reviewed the documentation https://docs.sentry.io/
- [X] I am using the latest SDK release https://github.com/getsentry/sentry-javascript/releases
How do you use Sentry?
Sentry Saas (sentry.io)
Which SDK are you using?
@sentry/nextjs
SDK Version
8.0.0-beta.4
Framework Version
No response
Link to Sentry event
No response
SDK Setup
import { init } from '@sentry/nextjs'; import { nodeProfilingIntegration } from '@sentry/profiling-node'; import { SENTRY_CAPTURE_RATE, SENTRY_DSN } from 'sentry.constants.mjs';
export function register() { if (process.env.NEXT_RUNTIME === 'nodejs') { init({ dsn: SENTRY_DSN, debug: false, tracesSampleRate: SENTRY_CAPTURE_RATE, profilesSampleRate: SENTRY_CAPTURE_RATE, environment: process.env.ENV, integrations: [nodeProfilingIntegration()], }); } if (process.env.NEXT_RUNTIME === 'edge') { init({ dsn: SENTRY_DSN, tracesSampleRate: SENTRY_CAPTURE_RATE, environment: process.env.ENV, }); } }
Steps to Reproduce
https://docs.sentry.io/platforms/javascript/guides/nextjs/migration/v7-to-v8/#opentelemetry-instrumentation 3 step says to remove instrumentation.node.ts. Tried it and it causes errors because now webpack emits one chunk for edge and node instrumentation. And it fails because node api is not available in edge.
Before i was using:
export async function register() {
if (process.env.NEXT_RUNTIME === 'nodejs') {
await import('./instrumentation.node');
}
if (process.env.NEXT_RUNTIME === 'edge') {
await import('./instrumentation.edge');
}
}
Should i use it? but i am not sure that this dynamic imports are good way to init sdk
Expected Result
Build without errors
Actual Result
next build
▲ Next.js 14.3.0-canary.28
- Environments: .env.local
- Experiments (use with caution): · webpackBuildWorker · instrumentationHook · preloadEntriesOnStart
Creating an optimized production build ... Failed to compile.
./node_modules/.pnpm/[email protected]/node_modules/detect-libc/lib/detect-libc.js:6:1 Module not found: Can't resolve 'child_process' 4 | 'use strict'; 5 |
6 | const childProcess = require('child_process'); | ^ 7 | const { isLinux, getReport } = require('./process'); 8 | const { LDD_PATH, readFile, readFileSync } = require('./filesystem'); 9 |
https://nextjs.org/docs/messages/module-not-found
Import trace for requested module: ./node_modules/.pnpm/@[email protected]/node_modules/@sentry/profiling-node/lib/esm/index.js ./src/instrumentation.ts
./node_modules/.pnpm/[email protected]/node_modules/detect-libc/lib/filesystem.js:6:1 Module not found: Can't resolve 'fs' 4 | 'use strict'; 5 |
6 | const fs = require('fs'); | ^ 7 | 8 | /** 9 | * The path where we can find the ldd
https://nextjs.org/docs/messages/module-not-found
Import trace for requested module: ./node_modules/.pnpm/[email protected]/node_modules/detect-libc/lib/detect-libc.js ./node_modules/.pnpm/@[email protected]/node_modules/@sentry/profiling-node/lib/esm/index.js ./src/instrumentation.ts
node:module
Module build failed: UnhandledSchemeError: Reading from "node:module" is not handled by plugins (Unhandled scheme).
Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "node:" URIs.
at /Users/seitasanov/ownProjects/profile/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]_@[email protected]_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/webpack/bundle5.js:28:401799
at Hook.eval [as callAsync] (eval at create (/Users/seitasanov/ownProjects/profile/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]_@[email protected]_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/webpack/bundle5.js:13:28858),
Import trace for requested module: node:module ./node_modules/.pnpm/@[email protected]/node_modules/@sentry/profiling-node/lib/esm/index.js
node:path
Module build failed: UnhandledSchemeError: Reading from "node:path" is not handled by plugins (Unhandled scheme).
Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "node:" URIs.
at /Users/seitasanov/ownProjects/profile/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]_@[email protected]_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/webpack/bundle5.js:28:401799
at Hook.eval [as callAsync] (eval at create (/Users/seitasanov/ownProjects/profile/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]_@[email protected]_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/webpack/bundle5.js:13:28858),
Import trace for requested module: node:path ./node_modules/.pnpm/@[email protected]/node_modules/@sentry/profiling-node/lib/esm/index.js
node:url
Module build failed: UnhandledSchemeError: Reading from "node:url" is not handled by plugins (Unhandled scheme).
Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "node:" URIs.
at /Users/seitasanov/ownProjects/profile/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]_@[email protected]_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/webpack/bundle5.js:28:401799
at Hook.eval [as callAsync] (eval at create (/Users/seitasanov/ownProjects/profile/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]_@[email protected]_rea_h4jxyf2ocyapuotaulkocqpl24/node_modules/next/dist/compiled/webpack/bundle5.js:13:28858),
Import trace for requested module: node:url ./node_modules/.pnpm/@[email protected]/node_modules/@sentry/profiling-node/lib/esm/index.js
Build failed because of webpack errors
Problem with @sentry/profiling-node integration
@r34son profiling will not work without a nodejs API, so I would recommend you remove this for edge runtime. I will need to look at how and if it's even possible to support this. I'm sorry if this isnt of much help.
@r34son profiling will not work without a nodejs API, so I would recommend you remove this for edge runtime. I will need to look at how and if it's even possible to support this. I'm sorry if this isnt of much help.
It is actually the problem, because of one chunk emited for both runtimes
It should be fine to inti the edge SDK with a dynamic import. If possible, try to leave the node SDK init synchronous.
Thanks!
If I init sdk this way:
import { init } from '@sentry/nextjs';
import { nodeProfilingIntegration } from '@sentry/profiling-node';
import { SENTRY_CAPTURE_RATE, SENTRY_DSN } from 'sentry.constants.mjs';
export async function register() {
if (process.env.NEXT_RUNTIME === 'nodejs') {
init({
dsn: SENTRY_DSN,
debug: false,
tracesSampleRate: SENTRY_CAPTURE_RATE,
profilesSampleRate: SENTRY_CAPTURE_RATE,
environment: process.env.ENV,
integrations: [nodeProfilingIntegration()],
});
}
if (process.env.NEXT_RUNTIME === 'edge') {
await import('./instrumentation.edge');
}
}
It somehow fails in turbopack:
Only works importing @sentry/profiling-node dynamically:
import { init } from '@sentry/nextjs';
import { SENTRY_CAPTURE_RATE, SENTRY_DSN } from 'sentry.constants.mjs';
export async function register() {
if (process.env.NEXT_RUNTIME === 'nodejs') {
const { nodeProfilingIntegration } = await import('@sentry/profiling-node');
init({
dsn: SENTRY_DSN,
debug: false,
tracesSampleRate: SENTRY_CAPTURE_RATE,
profilesSampleRate: SENTRY_CAPTURE_RATE,
environment: process.env.ENV,
integrations: [nodeProfilingIntegration()],
});
}
if (process.env.NEXT_RUNTIME === 'edge') {
await import('./instrumentation.edge');
}
}
This is actually a good insight. Thanks for raising this! I see this as a documentation issue we need to resolve.