Jared
Jared

Reputation: 3016

Cannot find module in AWS Lambda function

I'm getting this error when I upload and test my AWS Lambda function:

{
  "errorMessage": "Cannot find module 'elasticsearch'",
  "errorType": "Error",
  "stackTrace": [
    "Function.Module._load (module.js:276:25)",
    "Module.require (module.js:353:17)",
    "require (internal/module.js:12:17)",
    "Object.<anonymous> (/var/task/PublishElasticsearch.js:17:21)",
    "Module._compile (module.js:409:26)",
    "Object.Module._extensions..js (module.js:416:10)",
    "Module.load (module.js:343:32)",
    "Function.Module._load (module.js:300:12)",
    "Module.require (module.js:353:17)"
  ]
}

and the line of code that require()s the module:

var elasticsearch = require('elasticsearch')({
    host: config.ElasticHost
});

The function runs successfully the only issue is requiring my module.

I run npm install in the function directory and my ZIP that I upload is structured like this:

PublishElasticsearch.zip
    PublishElasticsearch.js
    node_modules
        elasticsearch

Does anyone know why I would be getting this error?

Upvotes: 9

Views: 26432

Answers (2)

Pa Ye
Pa Ye

Reputation: 1838

I had all modules listed under devDependencies. Moving everything over to dependencies solved it for me.

Upvotes: 4

cpanato
cpanato

Reputation: 429

you can try this anwser here: aws-lambda Cannot find module

looks like is the same problem you might have

Upvotes: -1

Related Questions