Module not found Serverless Lambda Error with webpack

Viewed 1147

I am working with AWS lambda functions. I have configured Webpack and babel for es6 or later js versions. Everything working well. I also implemented lambda layers for common nodejs modules. This is serverless.yml

layers:
  commonLayer:
    path: layers/common
    description: nodejs common modules

Here is lambda function second where commonLayer is being used

  second:
    handler: handlers/second.hello
    layers:
      - { Ref: CommonLayerLambdaLayer }
    events:
      - http:
          method: get
          path: second

I have written a function inside commonLayer to consume in second function but when I try to run sls deploy it gives this error

ERROR in ./handlers/first.js
Module not found: Error: Can't resolve '/opt/nodejs/palindrome' in '/Users/adamanjum/Documents/workspace/serverless/handlers'
 @ ./handlers/first.js 2:0-48 11:12-22

Project Repository

1 Answers

Your import path for palindrome is incorrect. In handlers/first.js, try changing import palindrome from '/opt/nodejs/palindrome'; to import palindrome from ../layers/common/nodejs/palindrome;.

Related