newrelic / newrelic-lambda-cli

A CLI to install the New Relic AWS Lambda integration and layers.
https://newrelic.com/products/serverless-aws-lambda
Apache License 2.0
47 stars 53 forks source link

Unable to resolve module file for NextJS #260

Closed mintavenger closed 8 months ago

mintavenger commented 8 months ago

Description

AWS Lambda fails to start with exception with NewRelic layer

2024-01-29T18:13:49.184Z undefined ERROR Uncaught Exception {"errorType":"Error","errorMessage":"Unable to resolve module file at /var/task/nextjssite-index with the following extensions: .cjs,.js","stack":["Error: Unable to resolve module file at /var/task/nextjssite-index with the following extensions: .cjs,.js"," at getFullyQualifiedModulePath (/opt/nodejs/node_modules/newrelic-lambda-wrapper/index.js:59:11)"," at getModuleWithRequire 

Steps to Reproduce

Use Newrelic CLI to install monitoring layer to Lambda function on AWS

Expected Behaviour

Metrics to appear and lambdas to execute

Relevant Logs / Console output

2024-01-29T18:13:49.184Z undefined ERROR Uncaught Exception {"errorType":"Error","errorMessage":"Unable to resolve module file at /var/task/nextjssite-index with the following extensions: .cjs,.js","stack":["Error: Unable to resolve module file at /var/task/nextjssite-index with the following extensions: .cjs,.js"," at getFullyQualifiedModulePath (/opt/nodejs/node_modules/newrelic-lambda-wrapper/index.js:59:11)"," at getModuleWithRequire (/opt/nodejs/node_modules/newrelic-lambda-wrapper/index.js:82:26)"," 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:1256:14)"," at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)"," at Module.load (node:internal/modules/cjs/loader:1119:32)"," at Module._load (node:internal/modules/cjs/loader:960:12)"," at Module.require (node:internal/modules/cjs/loader:1143:19)"," at require (node:internal/modules/cjs/helpers:119:18)"]}

Your Environment

  1. Using NextJS and SST framework
  2. Node.js 18 using arm64 architecture
  3. 1024 Mb memory, timeout 40 secs

Additional context

mrickard commented 8 months ago

@mintavenger Have you set the NEW_RELIC_USE_ESM environment variable for your function to true? ESM instrumentation requires a separate loading path, which is triggered by that env var. That's mentioned in the README for the Lambda layers: https://github.com/newrelic/newrelic-lambda-layers?tab=readme-ov-file#support-for-es-modules-nodejs

mintavenger commented 8 months ago

Thanks @mrickard for the link. I added NEW_RELIC_USE_ESM and I still get the error:

2024-01-30T18:16:04.143Z    undefined   ERROR   Uncaught Exception  
{
    "errorType": "Error",
    "errorMessage": "Unable to resolve module file at /var/task/nextjssite-index with the following extensions: .cjs,.js",
    "stack": [
        "Error: Unable to resolve module file at /var/task/nextjssite-index with the following extensions: .cjs,.js",
        "    at getFullyQualifiedModulePath (/opt/nodejs/node_modules/newrelic-lambda-wrapper/index.js:59:11)",
        "    at getModuleWithRequire (/opt/nodejs/node_modules/newrelic-lambda-wrapper/index.js:82:26)",
        "    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:1256:14)",
        "    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)",
        "    at Module.load (node:internal/modules/cjs/loader:1119:32)",
        "    at Module._load (node:internal/modules/cjs/loader:960:12)",
        "    at Module.require (node:internal/modules/cjs/loader:1143:19)",
        "    at require (node:internal/modules/cjs/helpers:119:18)"
    ]
}

I guess I will try the manual method and apply it to a few select lambdas...

https://github.com/newrelic/newrelic-lambda-layers?tab=readme-ov-file#manual-instrumentation-for-es-modules