MonkeyBonkey
MonkeyBonkey

Reputation: 47881

lambda not finding my node_modules

I'm trying to upload node_modules with my lambda but I'm getting "Cannot find module" error.

I've set up a real simple hello world js file with

var async = require('async');

And I have manually copied over a node_modules/async folder into the distribution - and I copy up the node_modules folder along with the hello world js file.

I do a very similar thing with my photo resizer lambda which takes node modules as well and that works. What's different that I'm doing wrong?

Upvotes: 4

Views: 4203

Answers (2)

Octopus
Octopus

Reputation: 8325

I'm putting this here as an alternative answer. I spent all afternoon trying to get my module to run in Lambda. I had the same structure as shown in the other answer but kept getting "Cannot find module" errors or Object.fs.readFileSync errors. It turns out that making sure all the js files are executable before zipping them was an important step that I was missing. It might also be of benefit to chown all the files to yourself as well.

sudo chmod +x *.js -R
sudo chown myself.myself * -R
zip -r lambda.zip .

Then upload the lambda.zip package and test.

Upvotes: 0

garnaat
garnaat

Reputation: 45856

The structure of the zip file is important. Let's say I have a javascript function contained in a file called foo.js that has dependencies on other node modules.

In my development environment, I would have a structure like this:

devdir/
    foo/
        foo.js
        node_modules/
            <the nodejs modules>

I then create a zip file called foo.zip structured like this:

$ unzip -vl foo.zip 
Archive:  foo.zip
 Length   Method    Size  Ratio   Date   Time   CRC-32    Name
 -------  ------  ------- -----   ----   ----   ------    ----
       0  Defl:N        0   0%  08-05-15 14:44  00000000  ./
    3047  Defl:N      981  68%  08-05-15 14:25  06e3e178 foo.js
       0  Defl:N        0   0%  08-03-15 13:37  00000000  node_modules/
       0  Defl:N        0   0%  08-03-15 13:37  00000000 node_modules/.bin/
     597  Defl:N      301  50%  03-05-15 14:29  9b0c2ba2  node_modules/.bin/uuid
       0  Defl:N        0   0%  07-16-15 08:32  00000000  node_modules/async/
    3454  Defl:N     1537  56%  06-28-15 18:37  967a5404  node_modules/async/CHANGELOG.md
<...>

Make sure your zip file is structured like this and you should be ok.

Upvotes: 6

Related Questions