newrelic/serverless-newrelic-lambda-layers

Cannot find module './newrelic-lambda-wrapper.js'

alessandroprudencio opened this issue · 6 comments

Has anyone already configured serverless + aws lambda + AdonisJS, but I can’t get it to work, I get the error below:

Cannot find module './newrelic-lambda-wrapper.js'

I’m using serverless-newrelic-lambda-layers and my serverless.yaml

org: xx
app: xx
service: xx
frameworkVersion: '3.1.1'

provider:
  name: aws

functions:
  AdonisApp:
    handler: build/lambda.adonisApp
    layers:
      - { Ref: NodeModulesLambdaLayer }
    events:
      - http:
          cors: true
          path: '/'
          method: any
      - http:
          cors: true
          path: '{proxy+}'
          method: any

package:
  patterns:
    - '!.build/**'
    - '!app/**'
    - '!bin/**'
    - '!commands/**'
    - '!config/**'
    - '!contracts/**'
    - '!coverage/**'
    - '!database/**'
    - '!start/**'
    - '!providers/**'
    - '!target/**'
    - '!test/**'
    - '!public/tmp/**'
    - '!tmp/**'
    - '!node_modules/**'
    - '!spec.js'
    - '!*.ts'
    - '!*.json'
    - '!*.lock'
    - '!.*'
    - '.adonis.json'
    - 'ace-manifest.json'
    - 'build/**'
    - '!build/japaFile.js*'
    - '!build/nyc.config.js*'
    - '!build/test'
    
layers:
  NodeModules:
    name: ${self:provider.apiName}-layer
    path: tmp/layers
    description: 'node_modules dependencies'

plugins:
  - serverless-newrelic-lambda-layers
  
custom:
  newRelic:
    debug: true
    accountId: xxxxx
    apiKey: xxxxxx
    enableExtension: true

Error:
ERROR Invoke Error {"errorType":"Error","errorMessage":"Cannot find module './newrelic-lambda-wrapper.js'\nRequire stack:\n- /var/task/s_AdonisApp.js\n- /var/runtime/UserFunction.js\n- /var/runtime/Runtime.js\n- /var/runtime/index.js","code":"MODULE_NOT_FOUND","requireStack":["/var/task/s_AdonisApp.js","/var/runtime/UserFunction.js","/var/runtime/Runtime.js","/var/runtime/index.js"],"stack":["Error: Cannot find module './newrelic-lambda-wrapper.js'","Require stack:","- /var/task/s_AdonisApp.js","- /var/runtime/UserFunction.js","- /var/runtime/Runtime.js","- /var/runtime/index.js"," at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)"," at Module._require.i.require (/var/task/serverless_sdk/index.js:9:73131)"," at require (internal/modules/cjs/helpers.js:101:18)"," at Object.<anonymous> (/var/task/s_AdonisApp.js:25:23)"," at Module._compile (internal/modules/cjs/loader.js:1085:14)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)"," at Module.load (internal/modules/cjs/loader.js:950:32)"," at Function.Module._load (internal/modules/cjs/loader.js:790:12)"," at Module.require (internal/modules/cjs/loader.js:974:19)"," at require (internal/modules/cjs/helpers.js:101:18)"]}

my package.json:

{
  "name": "xxxx",
  "version": "xxxx",
  "private": true,
  "scripts": {
    "dev": "node ace serve --watch --encore-args=\"--port 5000\"",
    "build": "node ace build --production",
    "start": "yarn build && node build/server.js",
    "package": "yarn layer:node_modules && sls package",
    "deploy": "yarn layer:node_modules && sls deploy --stage=v2 && tput bel",
  },
  "nyc": {
    "lines": 80
  },
  "devDependencies": {
    "@adonisjs/assembler": "^5.3.0",
    "@faker-js/faker": "7.6.0",
    "@istanbuljs/nyc-config-typescript": "^1.0.2",
    "@symfony/webpack-encore": "^1.8.1",
    "@types/jsdom": "^16.2.14",
    "@types/json2csv": "^5.0.3",
    "@types/mime-types": "^2.1.1",
    "@types/supertest": "^2.0.11",
    "adonis-preset-ts": "^2.1.0",
    "eslint": "^8.6.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-adonis": "^2.1.0",
    "eslint-plugin-prettier": "^4.0.0",
    "execa": "5.1.1",
    "get-port": "5.1.1",
    "git-commit-msg-linter": "^4.2.1",
    "japa": "^4.0.0",
    "jsdom": "^19.0.0",
    "ngrok": "^4.2.2",
    "nock": "^13.2.2",
    "nyc": "^15.1.0",
    "prettier": "^2.5.1",
    "rewiremock": "^3.14.3",
    "serverless": "^3.26.0",
    "serverless-newrelic-lambda-layers": "^3.4.0",
    "serverless-offline": "^8.3.1",
    "serverless-offline-sqs": "^6.0.0",
    "serverless-plugin-typescript": "^2.1.0",
    "source-map-support": "^0.5.21",
    "sqs-consumer": "^5.6.0",
    "supertest": "^6.2.1",
    "ts-node": "^10.4.0",
    "typescript": "~4.5",
    "websocket": "^1.0.34",
    "youch": "^3.0.0",
    "youch-terminal": "^2.0.0"
  },
  "dependencies": {
    "@adonisjs/auth": "^8.0.10",
    "@adonisjs/core": "^5.4.0",
    "@adonisjs/drive-s3": "^1.0.5",
    "@adonisjs/i18n": "^1.5.3",
    "@adonisjs/lucid": "^16.3.2",
    "@adonisjs/repl": "^3.1.0",
    "@adonisjs/view": "^6.1.1",
    "@satheler/s12r": "^0.2.7",
    "@types/htmlparser2": "^3.10.3",
    "adonis-lucid-soft-deletes": "^1.3.3",
    "colors": "^1.4.0",
    "csv": "^6.0.5",
    "csv-parse": "^5.0.4",
    "dayjs": "^1.11.7",
    "file-extension": "^4.0.5",
    "jimp": "^0.16.1",
    "json2csv": "^5.0.7",
    "jsonwebtoken": "^8.5.1",
    "jszip": "^3.9.1",
    "luxon": "^2.3.0",
    "mime-types": "^2.1.35",
    "mysql2": "^2.3.3",
    "nanoid": "^3.3.1",
    "open-graph-scraper": "^4.11.0",
    "parameterize": "^1.0.0",
    "pino-pretty": "^7.3.0",
    "proxy-addr": "^2.0.7",
    "reflect-metadata": "^0.1.13",
    "rollbar": "^2.24.0",
    "serverless-aws-static-file-handler": "^3.1.0",
    "unirest": "^0.6.0",
    "uuid": "^8.3.2",
    "xlsx": "https://cdn.sheetjs.com/xlsx-0.18.10/xlsx-0.18.10.tgz"
  }
}


@alessandroprudencio While I'm not familiar with Adonis, I do notice that in the stack trace there's a reference to './newrelic-lambda-wrapper.js'. Typically the NR layer-installed wrapper would be set as the handler in the AWS Lambda environment, and not with ./ as a prefix. Dependencies installed from the layer are installed to /opt/nodejs/node_modules/ and not in /var/task where user code runs.

The AdonisJs is framework for node js https://docs.adonisjs.com/guides/introduction

I don't know why it's pulling ./newrelic-lambda-wrapper.js with ./ as a prefix.

but remembering that I have a layer of node_modules, I don't know if it interferes with something...

what do I do now @mrickard ?

@alessandroprudencio I don't think the layer of node_modules is interfering with anything. What you'll likely need to do is find out where that /. prefix is being defined. You could open a support ticket with NR GTS--they can help you verify your function's environment variables and configuration where it might impact agent setup.

In your function code, do you import dependencies with that ./ prefix? We recommend not specifying directory path that way, so that the agent can leverage the Lambda environment's NODE_PATH for finding functions and dependencies.

I am facing this exact issue as well. I am trying to attach the serverless-newrelic-lambda-layers plugin to my serverless framework function but I am getting the below error.

ERROR Error: Cannot find module './newrelic-lambda-wrapper.js' Require stack: - /var/task/s_process.js - /var/runtime/UserFunction.js - /var/runtime/Runtime.js - /var/runtime/index.js at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15) at Module._require.i.require (/var/task/serverless_sdk/index.js:9:73131) at require (internal/modules/cjs/helpers.js:101:18) at Object.&lt;anonymous&gt; (/var/task/s_process.js:25:23) at Module._compile (internal/modules/cjs/loader.js:1085:14) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10) at Module.load (internal/modules/cjs/loader.js:950:32) at Function.Module._load (internal/modules/cjs/loader.js:790:12) at Module.require (internal/modules/cjs/loader.js:974:19) at require (internal/modules/cjs/helpers.js:101:18) { code: 'MODULE_NOT_FOUND', requireStack: [ '/var/task/s_process.js', '/var/runtime/UserFunction.js', '/var/runtime/Runtime.js', '/var/runtime/index.js' ] }

Trace Log:

serverless_sdk/index.js
9 */(function(){var i="Expected a function",o="__lodash_placeholder__",s=[["ary",128],["bind",1],["bi
    ^&nbsp;Error&nbsp;Cannot find module './newrelic-lambda-wrapper.js' Require stack: - /var/task/s_process.js - /var/runtime/UserFunction.js - /var/runtime/Runtime.js - /var/runtime/index.js
s_process.js
25 const userHandler = require('./newrelic-lambda-wrapper.js');

I assume the problem arises because it is importing the module with the "./" when it shouldn't. I am not manually importing the plugin anywhere nor have I made any code changes to install this plugin. All changes have been sole to the serverless.yml file. Attaching my serverless.yml below for reference as well.

org: XXX
app: XXX
service: XXX

provider:
  name: aws
  runtime: nodejs16.x
  timeout: 900
  environment:
    NODE_ENV: dev

plugins:
  - serverless-offline
  - serverless-plugin-typescript
  - serverless-newrelic-lambda-layers

custom:
  newRelic:
    accountId: ${param:NEW_RELIC_ACCOUNT_ID}
    apiKey: ${param:NEW_RELIC_API_KEY}
    enableFunctionLogs: true

functions:
  process:
    handler: app/handler.process
    layers:
      - ${param:XXX}
    events:
      - http:
          path: process
          method: post
    environment:
      XXX: ${param:XXX}

Any solutions that I should be trying?

The issue will not be fixed.