Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Agent not working with Yarn Pnp + NextJS #4000

Open
1 of 3 tasks
cmartineauOptimum opened this issue Apr 30, 2024 · 5 comments
Open
1 of 3 tasks

Agent not working with Yarn Pnp + NextJS #4000

cmartineauOptimum opened this issue Apr 30, 2024 · 5 comments

Comments

@cmartineauOptimum
Copy link

cmartineauOptimum commented Apr 30, 2024

Describe the bug

When running the apm agent following the documentation for a nextjs project, using Yarn Berry with Pnp, I get this error :

Error: Cannot find module 'elastic-apm-node/start-next.js'

image

To Reproduce

Steps to reproduce the behavior:

  1. clone this repository and navigate to the nextjs example.
npm install --global yarn
yarn install
yarn set version berry
yarn install
  1. Make sure you are using pnp by modify this line : nodeLinker: pnp in .yarnrc.yml
yarn install
yarn run dev

Expected behavior

I expected the apm to start.

Environment (please complete the following information)

  • OS: [e.g. Linux]
  • Node.js version: v18.18.0
  • APM Server version: v8.12.0
  • Agent version: v4.5.3

How are you starting the agent? (please tick one of the boxes)

  • Calling agent.start() directly (e.g. require('elastic-apm-node').start(...))
  • Requiring elastic-apm-node/start from within the source code
  • Starting node with -r elastic-apm-node/start
@david-luna
Copy link
Member

Hi @cmartineauOptimum,

basically what happens is that Yarn + pnpm is adding a custom loader for its cache under the hood. To do soo it sets the environment variable NODE_OPTIONS to a certain value.

The getting started guide assumes this var is not set so the npm scripts

"dev": "NODE_OPTIONS=--require=elastic-apm-node/start-next.js next dev"

overriding its value and making the yarn + pnpm loader unavailable.

You can change the script to append the required value into node options. This is an example

"dev": "NODE_OPTIONS=\"$NODE_OPTIONS --require=elastic-apm-node/start\" node next dev"

This will maintain reference to the required loaders and append the agent start script at the end.

Cheers!

@david-luna david-luna removed the triage label May 3, 2024
@cmartineauOptimum
Copy link
Author

Hello @david-luna,

I have tried your suggestion and it did work in this repository with the nextjs example.

When it came to try it with my project, I had this error :

Error: Qualified path resolution failed: we looked for the following paths, but none could be accessed.

Source path: /workspace/next
Not found: /workspace/next

it seem it wasn't able to start next. I change your suggestion to

"dev": "NODE_OPTIONS=\"$NODE_OPTIONS --require=elastic-apm-node/start-next.js\" next dev "

This seem to be working. do you see any issue with the change I made ?

@david-luna
Copy link
Member

do you see any issue with the change I made ?

Maybe we weren't using the same yarn + pnpm version and the resolution differs a little bit. It is okay to use the extension.

@david-luna
Copy link
Member

Hi @cmartineauOptimum

As per your last comment in assume the issue is fixed. Should I proceed to close it?

@cmartineauOptimum
Copy link
Author

Yes, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants