Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

705
Vistas
AWS lambda layers error when call API "cannot find module"

I try to use layers of AWS Lambda, watched a tutorial about it, but I get an error "cannot find module ..."

service: aws-nodejs 

package:
  exclude:
    - .gitignore
    - package.json
    - .git/**

provider:
  name: aws
  profile: sandbox
  runtime: nodejs12.x

layers:
  testLayer:
    path: testLayer
    compatibleRuntimes:
      - nodejs12.x
    allowedAccounts:
      - '*'

functions:
  hello:
    handler: handler.hello
    layers:
      -  arn:aws:lambda:us-east-1:*:layer:testLayer:15
    events:
      - http:
          path: test
          method: get
          cors: true

When i deploy it, i don't have any errors in my terminal, and on AWS, i see my layer and when i download it, i have my package.json with moment dependency, and the node_modules folder with moment

my handler.js looks like this :

'use strict';
module.exports.hello = async (event, context) => {
    const moment = require('moment')
    const a = moment('2016-01-01')
  return {
    statusCode: 200,
    body: JSON.stringify({
      message: 'Hey' + a
    }),
  };
};

My files structure :

testLayer/
   node_modules/
      moment/
   package.json
serverless.yml
handler.js
package.json

Do you have any idea of what i doing wrong ?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Yes, it's possible that your node modules are there in your lambda layer and yet lambda is throwing error of "cannot find module..".

This may be happening because the zip that you have created is not as per directory structure mentioned in AWS documentation.

As per official documentation :

Including Library Dependencies in a Layer You can move runtime dependencies out of your function code by placing them in a layer. Lambda runtimes include paths in the /opt directory to ensure that your function code has access to libraries that are included in layers.

To include libraries in a layer, place them in one of the folders supported by your runtime.

Node.js – nodejs/node_modules, nodejs/node8/node_modules (NODE_PATH)

Example AWS X-Ray SDK for Node.js

nodejs/node_modules/aws-xray-sdk

Make sure your zip contains correct directory structure else try importing your modules from /opt/your_node_module_directory

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda