newrelic / newrelic-node-examples

Working examples of New Relic Node.js agent features
Apache License 2.0
15 stars 22 forks source link

Newrelic import failing when user from middleware in NextJS app router example #276

Closed limoragni closed 1 week ago

limoragni commented 1 month ago

Description

When using the nextjs-app-router example importing the newrelic module is not working from a middleware file. The serverComponentsExternalPackages prevents imports from breaking when using the module from server components, but is not fixing imports from a middleware file.

Steps to Reproduce

  1. Create a middleware.js file in the root of the nextjs-app-router example
  2. Import the newrelic module from it
    
    import { NextResponse } from "next/server";

// This function can be marked async if using await inside export function middleware(request) { newrelic.setTransactionName(request.url); return NextResponse.next(); }


3. Run `npm run dev`
4. Navigate to any page
5. Import will break and give an error

⨯ ./node_modules/newrelic/LICENSE Module parse failed: Unexpected token (1:40) You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders


## Expected Behavior

The module should be properly imported and able to be used from the middleware file

## Relevant Logs / Console output

./node_modules/newrelic/LICENSE Module parse failed: Unexpected token (1:40) You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders

                             Apache License

| Version 2.0, January 2004 | http://www.apache.org/licenses/

Import trace for requested module: ./node_modules/newrelic/LICENSE ./node_modules/newrelic/ sync ^.\/.*$ ./node_modules/newrelic/index.js ⚠ ./node_modules/@newrelic/security-agent/lib/instrumentation-security/core/sec-utils.js Critical dependency: the request of a dependency is an expression



## Your Environment
- MacOs
- Chrome
workato-integration[bot] commented 1 month ago

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

jsumners-nr commented 1 month ago

I don't know what the difference is. We would welcome suggestions to resolve the problem. Have you reached out to the Next team to see if they know how to resolve it?

limoragni commented 1 month ago

Thanks for the reply. I haven't reached out to the NextJS team. I've been learning more about this and I think the difference is that middleware calls are restricted to the Edge Runtime only. This means the full NodeJS API is not available. I'm not sure if that's the reason for this error though, since it doesn't even get to import the module.

I found this related issue

In my view this is very limiting if you want to use newrelic with NextJS. In my case I just want to be able to add custom attributes on all requests so I can have user ids sent to newrelic, and middleware would be the right place to do that.

jsumners-nr commented 1 month ago

Unfortunately, this exceeds our knowledge of Next. We put together this example based upon their documentation and Internet sleuthing. I believe the issue is with whatever Next does to "bundle" the resources being used by the application. If you're able to get the Next team to provide an answer, we'd love to learn from it.

bizob2828 commented 1 month ago

@limoragni unfortunately our instrumentation is limited due to how Next.js as a project has evolved. You may want to check out these FAQs we have drafted to help answer the nuance