Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 648
Jared Avatar asked Aug 15 '16 07:08

Jared


1 Answers

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

like image 102
Pavel Ye Avatar answered Oct 20 '22 09:10

Pavel Ye