newrelic / node-newrelic

New Relic Node.js agent code base. Developers are welcome to create pull requests here, please see our contributing guidelines. For New Relic technical support, please go to http://support.newrelic.com.
Apache License 2.0
968 stars 399 forks source link

Implementing newrelic in a NextJS app within the pages/_app.tsx module #1960

Closed p1xelcure closed 9 months ago

p1xelcure commented 9 months ago

Description

I am trying to upgrade our node-newrelic package from 8.14.1 to 11.9.0 on a NextJS (v14) app and running into issues around node packages not being available when building. I have the following pages/_app.tsx file:

import React, { useEffect } from 'react';
import { isOnServer } from '@whitelabel/helpers/utils';

if (isOnServer) {
  // newrelic is needed here for production server perfomance monitoring
  require('newrelic');
}

function MyApp({ Component, pageProps }: AppProps) {
  // and continued
}

When updating the package, and running a build on the app, i experience the following issues: Screenshot 2024-01-22 at 10 12 31 AM

From my understanding, the build is running into issues accessing node libraries that are not available because it thinks it is on the client. Why may this be happening? Is this a bug within the new version of newrelic? This same logic was working correctly on v 8.14.1 but with the new updated package, it is causing problems aren't libraries not being available.

Am I approaching this in the correct way? This pages/_app.tsx module runs both on the server and the client, which is why we are checking to see if we are on the server. isOnServer is simply a variable storing the value of typeof window === 'undefined'. This issue is NOT happening when running the project locally, only during build time.

Expected Behavior

Troubleshooting or NR Diag results

The app builds correctly, and newrelic is required when pages/_app.tsx is being ran on the server side

Steps to Reproduce

Your Environment

*Node version: Node vs v18.17.0

Additional context

I was also investigating setting up https://github.com/newrelic/newrelic-node-nextjs. However, we are not using getServerSideProps or any of these server side functions to fetch data on the server, so I have opted out of it. But, I'm wondering if this is something we should be using for loading new relic instead of the node-newrelic package. Or both?

Any help would be greatly appreciated, thank you!

workato-integration[bot] commented 9 months ago

https://new-relic.atlassian.net/browse/NR-217662

p1xelcure commented 9 months ago

I've identified this as an issue outside of newrelic, please disregard