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.

Nextjs "server Sentry.Init()" is never called if `npm run build` happened in different environment than hosting

See original GitHub issue

Package + Version

  • @sentry/browser
  • @sentry/node
  • raven-js
  • raven-node (raven for node)
  • other: @sentry/nextjs

Version:

6.5.1

Description

I use Github Actions to build my nextjs application, then deploy artifacts to a NodeJS Server hosted elsewhere. https://nextjs.org/docs/deployment#nodejs-server

During npm run build @sentry/nextjs creates a file .env.local and sets an environment variable SENTRY_SERVER_INIT_PATH. The value of said variable is an absolute path i.e. C:\some_path_to_project\.next\server\sentry\initServerSDK.js

During npm run start process.env.SENTRY_SERVER_INIT_PATH is used to require initServerSDK.js. However, npm run start is called after artifacts have been uploaded to a different environment running a NodeJS server. Because of this, the absolute path of process.env.SENTRY_SERVER_INIT_PATH is no longer valid.

initServerSDK.js cannot be found and Sentry.Init() is never called - serverside logging does not happen.

Source code https://github.com/getsentry/sentry-javascript/blob/e44bc6391976ab2ae759ca015bf774a6b9d2472f/packages/nextjs/src/utils/config.ts#L161

https://github.com/getsentry/sentry-javascript/blob/e44bc6391976ab2ae759ca015bf774a6b9d2472f/packages/nextjs/src/utils/instrumentServer.ts#L117

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
jtn-junedeskcommented, Jun 12, 2021

Thank you for a great reply @AbhiPrasad - and for providing references.

However, from reading next-server.ts, my understanding is that next.js is already handling those scenarios (path locations, distDir, serverless) when they expose .serverBuildDir. If we use the property which next.js has already exposed, my feeling is that this would be the most safe option. This means that we have to make zero assumptions and give next.js 100% responsibility for telling us where the directory is.

If they add more options in the future, I would expect .serverBuildDir to reflect this.

You can see their current implementation here: https://github.com/vercel/next.js/blob/0402fc459eb12a775372172a351f3a3f4fee83e3/packages/next/next-server/server/next-server.ts#L243-L246

I case this makes sense, I would gladly dedicate some time to creating a PR 😃

2reactions
jtn-junedeskcommented, Jun 5, 2021

@AbhiPrasad Looking through the code, I am a bit curious why you have chosen a 2 step model (create/consume) which creates a disk file dependency (.env.local).

Could instrumentServer.ts not simply do this:

import * as path from 'path';
import { SERVER_SDK_INIT_PATH } from './utils/config';

[...]

const serverSdk = path.join(this.server.serverBuildDir, config.SERVER_SDK_INIT_PATH);
require(serverSdk);

With this solution you would not need to create .env.local during build and could remove some complexity 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

sentry/nextjs not working on server · Issue #3691 - GitHub
I do use the withSentry wrapper around my handlers, but it appears that Sentry.init never gets called despite sentry.server.config.js being ...
Read more >
Manual Setup for Next.js - Sentry Documentation
If you can't (or prefer not to) run the configuration step, you can follow the instructions below to configure your application.
Read more >
Getting build error occurred when running npm run build
In Config/index.js it is expecting the API server is hosted somewhere "https://yourwebsite.com". As a workaround,. Modify "https://yourwebsite.
Read more >
Going to Production - Next.js
Before taking your Next.js application to production, here are some recommendations to ensure the best user experience.
Read more >
How to Build Scalable Architecture for your Next.js Project
All code from this tutorial as a complete package is available in this repository. If you find this... Tagged with nextjs, webdev, ...
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