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

Can't resolve 'elastic-apm-node' #189

Open
aniketbiprojit opened this issue Jul 4, 2024 · 4 comments
Open

Can't resolve 'elastic-apm-node' #189

aniketbiprojit opened this issue Jul 4, 2024 · 4 comments
Assignees

Comments

@aniketbiprojit
Copy link

⚠ ./node_modules/.pnpm/@elastic[email protected]/node_modules/@elastic/ecs-winston-format/index.js
Module not found: Can't resolve 'elastic-apm-node' in '.pnpm/@elastic[email protected]/node_modules/@elastic/ecs-winston-format'

Should I need to install this? I am not using elastic-apm-node anywhere.

This happens when I trying to integrate with Nextjs. Runs fine otherwise.

export const logger = winston.createLogger({
  format: ecsFormat(),
  transports: [
    new winston.transports.Console(),
    new winston.transports.File({ filename: `${outLogPath}/out-json.log` }),
  ],
});

This seems to be silently ignored in js file but I still get this error logged in NextJS logs.

Any ideas on how to suppress this error?

image
@trentm
Copy link
Member

trentm commented Aug 1, 2024

@aniketbiprojit Sorry about the delay in responding!

As you have found the usage of elastic-apm-node in the @elastic/ecs-winston-format module is meant to be silent if it cannot find that module.

You say you are using Next.js. That often means that a bundler is being used to produce a single .js file (or set of .js files) that are being run, instead of using the tree of files under ./node_modules/.... Do you know if that is the case for you?

Can you show the actual .js code that is doing that elasticApm = require('elastic-apm-node') call? Is it actually ./node_modules/.pnpm/https://github.com/[email protected]/node_modules/@elastic/ecs-winston-format/index.js or is that a source-map'ped point from a large bundle JS file?

Are you getting that Module not found: Can't resolve 'elastic-apm-node' at build/compile time? Or at runtime?
From your comment "but I still get this error logged in NextJS logs." ... I'm guessing this is at runtime.

If this is in a bundle, I'd love to see that bundle file. It may be that you cannot share the whole thing, if it has proprietary code in it, however.

@trentm trentm self-assigned this Aug 1, 2024
@aniketbiprojit
Copy link
Author

Hi. I kind of gave up on the package and wrote my own formatter for the same.

So the error was at compile time (dev and build processes). Never reached runtime.

@aniketbiprojit
Copy link
Author

Also when I installed elastic-apm-node it was throwing the same error for missing deps which are imported in elastic-apm-node.

@andrewstec
Copy link

andrewstec commented Nov 10, 2024

Great library, but this is making a lot of noise. I am using webpack and pnp as well.

WARNING in ./.yarn/cache/@elastic-ecs-winston-format-npm-1.5.3-67d8db26d9-19efeefe8b.zip/node_modules/@elastic/ecs-winston-format/index.js 33:15-42
Module not found: Error: Can't resolve 'elastic-apm-node' in '/Users/pastas/repos/spaghetti/yummyfood/backend/.yarn/cache/@elastic-ecs-winston-format-npm-1.5.3-67d8db26d9-19efeefe8b.zip/node_modules/@elastic/ecs-winston-format'
 @ ./src/core/utils/logger.ts 19:29-67
 @ ./src/core/middleware/loggingMiddleware.ts 4:17-43
 @ ./src/core/server.ts 8:28-69
 @ ./src/server.ts 7:33-57

This was taken from the bundle .js file:

// We will query the Elastic APM agent if it is available.
let elasticApm = null
try {
  elasticApm = __webpack_require__(Object(function webpackMissingModule() { var e = new Error("Cannot find module 'elastic-apm-node'"); e.code = 'MODULE_NOT_FOUND'; throw e; }()))
} catch (ex) {
  // Silently ignore.
}

I see it being requried and throwing an error. It never loads, but this "silent" code block is not so silent. Bundlers are going to listen for these errors. This does not appear to be standard behaviour for the npm/node ecosystem. I haven't seen warnings from other packages during the bundling process. My guess is that you should not try to load it and instead determine if it is there by checking first.

Hopefully this info helps. I'm thinking of gutting this library and writing my own too, although I'd rather use this and support official Elastic stuff if this could be fixed.

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

3 participants