martes, 5 de octubre de 2021

MODULE_NOT_FOUND using New Relic with Nest (NestJS) 8

When adding the newrelic.js file in the root of the NestJS project, and then building it and running it in prod mode:

> node dist/main

node:internal/modules/cjs/loader:936

  throw err;

  ^

Error: Cannot find module '/.../apps/backend/dist/main'

    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)

    at Function.Module._load (node:internal/modules/cjs/loader:778:27)

    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)

    at node:internal/main/run_main_module:17:47 {

  code: 'MODULE_NOT_FOUND',

  requireStack: []

}

In this case, tsc compiles just the newrelic.js file in your dist folder.


Move the newrelic.js to the same directory as your main.ts file (normally src/).

Complete installation instructions: https://discuss.newrelic.com/t/new-relic-for-nestjs/91812. I didn't follow all those for Nest 8 though. In my case:

  1. Same.
  2. Same.
  3. Same.
  4. Same. It didn't work with import _ from 'newrelic';. In that way, it got stripped from the compiled JS.
  5. 6. 7. I didn't need to. The newrelic.js automatically ended up in the dist/ folder.