NextJS - Error: Cannot find module ... initServerSDK.js

I’ve just integrated Sentry into my NextJS project. I frequently (3-4 times out of 5) see this error upon building/starting the project:

[Sentry] Could not initialize SDK. Received error:
Error: Cannot find module '/Users/[...]/.next/server/sentry/initServerSDK.js'
Require stack:
- /Users/[...]/node_modules/@sentry/nextjs/dist/utils/instrumentServer.js
- /Users/[...]/node_modules/@sentry/nextjs/dist/index.server.js
- /Users/[...]/next.config.js
- /Users/[...]/node_modules/next/dist/next-server/server/config.js
- /Users/[...]/node_modules/next/dist/server/next.js
- /Users/[...]/node_modules/next/dist/server/lib/start-server.js
- /Users/[...]/node_modules/next/dist/cli/next-dev.js
- /Users/[...]/node_modules/next/dist/bin/next

The file does exist at the specified path, at least once the final build is done. Maybe it’s a timing issue and the file isn’t there when the init happens but shows up after? The SDK integration does still seem to work (i.e., I see the test errors show up on the dashboard) for now. I jut want to make sure there isn’t a step I’m missing somewhere that will cause problems later. Thanks!

2 Likes

+1 Happens to me too

It’s weird but since I’ve changed the path to have a leading “./” this went away, might happen on other people’s machines tho, because it gets back to the state before once the build is run.

Default path

SENTRY_SERVER_INIT_PATH=.next/server/sentry/initServerSDK.js

Path with leading ./

SENTRY_SERVER_INIT_PATH=./.next/server/sentry/initServerSDK.js