9

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?

2
  • Sometimes I've noticed that if there's an error in the module, node will report that the module is missing. Double check your elasticsearch module that there are no errors on load, and also verify that any dependencies it requires is also part of your zip. Commented Aug 15, 2016 at 20:41
  • Can you make sure you have elasticsearch as one of the dependencies in your package.json. Commented Oct 10, 2018 at 13:29

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

-1

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

looks like is the same problem you might have

1 Comment

This is an entirely different issue; the question you linked is unable to find the handler file (PublishElasticsearch.js, in this case), while this question is about finding a dependency. (That may sound like a nitpick, but it boils down to "the solutions to the linked question are entirely irrelevant to this situation.")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.