elastic / ecs-logging-nodejs

https://www.elastic.co/guide/en/ecs-logging/nodejs/master/intro.html
Apache License 2.0
68 stars 39 forks source link

"Can't resolve 'elastic-apm-node'" error when used in a Next.js app #187

Closed petter closed 4 months ago

petter commented 4 months ago

I'm trying to use @elastic/ecs-pino-format in my Next.js app, and it's working mostly fine. However, whenever a logger instance is created I get the following error in my console:

 ⚠ ../../node_modules/@elastic/ecs-pino-format/index.js
Module not found: Can't resolve 'elastic-apm-node' in '/Users/petter/workspace/domstol/lovisa_core/node_modules/@elastic/ecs-pino-format'

Import trace for requested module:
../../node_modules/@elastic/ecs-pino-format/index.js
../../shared_library/frontend/logging/src/server.ts
../../shared_library/frontend/feature-toggles/src/backend.ts
../../shared_library/frontend/feature-toggles/src/index.ts
./src/app/layout.tsx

This seems to only happen during development in a Next.js app not using turbopack, so it's not a huge issue. Is there any way to stop these log messages?

Also seems to happen with the @elastic/ecs-winston-format package.

Reproduction

CodeSandbox

petter commented 4 months ago

I'm guessing it comes from attempting to requiring the package during instantiation. Would a require.resolve() check be a workaround for this? 🤔

petter commented 4 months ago

I've looked some more into this. It seems that whenever there is a require somewhere in a file, webpack will try to resolve it. Even if the require expression is never run. Because of this there doesn't seem to be a simple fix for this issue.

Since this is only an issue in Next.js with webpack (AFAIK) it isn't a big deal for us at least since turbopack will soon be the new standard anyways.