4

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?

MonkeyBonkey
  • 46,433
  • 78
  • 254
  • 460
  • My guess it is has to do with packaging. try this example... it is very concrete. build from there: http://docs.aws.amazon.com/lambda/latest/dg/walkthrough-s3-events-adminuser-create-test-function-create-function.html – Jason Mcmunn Aug 10 '15 at 19:51

2 Answers2

6

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.

garnaat
  • 44,310
  • 7
  • 123
  • 103
  • 1
    the error was on my part - my zip gulp task was leaving out the package.json files – MonkeyBonkey Aug 12 '15 at 17:29
  • Thanks for this answer @garnaat. For the sake of posterity, I am finding that Lambda prefers dependencies to be at the root level and not in a the conventional `node_modules` directory at all. – W4t3randWind Jan 09 '17 at 16:13
  • When you say *prefer*, what exactly do you mean? Is it not working for you with the ``node_modules`` directory? – garnaat Jan 09 '17 at 16:16
0

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.

Octopus
  • 8,075
  • 5
  • 46
  • 66