newrelic/serverless-newrelic-lambda-layers

"Unable to import module 'handler'

navihtot opened this issue · 5 comments

Hi, I'm using:

  • TypeScript
  • node.js 20
  • serverless-esbuild

I get next error:

ERROR	Uncaught Exception 	
{
    "errorType": "Error",
    "errorMessage": "Unable to import module 'handler'",
    "stack": [
        "Error: Unable to import module 'handler'",
        "    at handleRequireImportError (/opt/nodejs/node_modules/newrelic-lambda-wrapper/index.js:42:12)",
        "    at getModuleWithRequire (/opt/nodejs/node_modules/newrelic-lambda-wrapper/index.js:87:11)",
        "    at getHandlerSync (/opt/nodejs/node_modules/newrelic-lambda-wrapper/index.js:127:23)",
        "    at Object.<anonymous> (/opt/nodejs/node_modules/newrelic-lambda-wrapper/index.js:116:46)",
        "    at Module._compile (node:internal/modules/cjs/loader:1376:14)",
        "    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)",
        "    at Module.load (node:internal/modules/cjs/loader:1207:32)",
        "    at Module._load (node:internal/modules/cjs/loader:1023:12)",
        "    at Module.require (node:internal/modules/cjs/loader:1235:19)",
        "    at require (node:internal/modules/helpers:176:18)"
    ]
}

I've tried with manualWrapping: true and wrapping it in the code, but then I get:

{
    "errorType": "Runtime.ImportModuleError",
    "errorMessage": "Error: Cannot find module '../../node_modules/newrelic/index.js'\nRequire stack:\n- /var/task/handler.js\n- /var/runtime/index.mjs",
    "stack": [
        "Runtime.ImportModuleError: Error: Cannot find module '../../node_modules/newrelic/index.js'",
        "Require stack:",
        "- /var/task/handler.js",
        "- /var/runtime/index.mjs",
        "    at _loadUserApp (file:///var/runtime/index.mjs:1087:17)",
        "    at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:21)",
        "    at async start (file:///var/runtime/index.mjs:1282:23)",
        "    at async file:///var/runtime/index.mjs:1288:1"
    ]
}

I'm not sure what am I doing wrong. Serverless.yml part:

custom:
  esbuild:
    sourcemap: true
    exclude: ["./node_modules/newrelic/*"]
  newRelic:
    accountId: ${env:NEW_RELIC_ACCOUNT_ID, "DUMMY"}
    apiKey: ${env:NEW_RELIC_API_KEY, "DUMMY"}
    debug: true
    enableExtension: true
    enableFunctionLogs: true
    enableExtensionLogs: true
    cloudWatchFilter: "*"
    logLevel: info
    manualWrapping: true
    stages:
      - stage
      - production

and manual wrapping I've tried:

import * as newrelic from "newrelic";

export const handle: SQSHandler = newrelic.setLambdaHandler(async (event: SQSEvent): Promise<void> => {

@navihtot Sorry to hear you're having trouble. It's hard to tell the source from what you've provided, but from the ../s in the error stack, it could be how you've defined the location of your handler. The AWS Lambda container makes use of NODE_PATH to find layer-installed dependencies, and we've seen that defining the handler path starting from ./ can complicate how the path is interpreted.

I'm not familiar with esbuild, but you may want to inspect the zip file Serverless produces: if esbuild transpiles to cjs, you might not have a deployed mjs handler file.

You shouldn't have to exclude newrelic, unless you've defined it as a dependency in your local project. I'd recommend defining it as one of your devDependencies, so it's not bundled in your deployment package.

I'd recommend opening a ticket with Support, who can help you work through common setup issues: https://support.newrelic.com/s/

Thank you for your reply. I've tried with different approaches. With and without manual wrapping. I managed to get my code to execute without errors. However the "invocations" still don't end in new relic. I've submitted the ticket for the support as well with more detail.
In the end, after all tries, it looks to me like maybe the new relic extension works but doesn't find anything to send for some reason

Closing this issue as you created support request for this.

closing this issue