44

As my title explains I am getting the following error:

 {
  "errorMessage": "Cannot find module 'index'",
  "errorType": "Error",
  "stackTrace": [
    "Function.Module._resolveFilename (module.js:338:15)",
    "Function.Module._load (module.js:280:25)",
    "Module.require (module.js:364:17)",
    "require (module.js:380:17)"
  ]
}

I have tried both solutions provided in creating-a-lambda-function-in-aws-from-zip-file and simple-node-js-example-in-aws-lambda

My config currently looks like:enter image description here

and my file structure is: enter image description here

and my index.js handler function looks like :

exports.handler = function(event, context) {

What else could be causing this issue aside from what was stated in those two answers above? I have tried both solutions and I have also allocated more memory to the function just incase thats why it couldn't run.

EDIT - For the sake of trying, I created an even simpler version of my original code and it looked like this:

var Q = require('q');
var AWS = require('aws-sdk');
var validate = require('lambduh-validate');
var Lambda = new AWS.Lambda();
var S3 = new AWS.S3();




theHandler = function (event, context) {

  console.log =('nothing');

}

exports.handler = theHandler();

And yet still does not work with the same error?

1
  • Having exactly the same issue.... with error Cannot find module '/var/task/index' during lamdba bootstrap. Thanks for asking the question! Commented Jan 26, 2017 at 14:51

9 Answers 9

77

Try zipping and uploading the contents of the folder lambda-create-timelapse. Not the folder itself.

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

3 Comments

Im not sure this really answers the question. Perhaps you could give a bit more detail (as to why this is a provisioning rather than coding issue)
This was actually the solution I ended up using on my own. Its very annoying how amazon words this, so it wasn't easy to realize!
As official documents say, at the end of the page you can see Important: you zip the folder content, not the folder itself docs.aws.amazon.com/lambda/latest/dg/…
25

If this was unclear for anyone else, here are the steps:

Step 1 Navigate to the folder of your project, and open that folder so that you are inside the folder: Folder

Step 2 Select all of the images you want to upload into to Lambda: Folder with selected files

Step 3 Right-click and compress the files you have selected: Right-click menu with compress option highlighted


This will give you a .zip file, which is the file you need to upload to Lambda:

Archive.zip output


There are a lot of ways to automate this, but this is the manual procedure.

1 Comment

Amazing!!! saved me a lot of time
14

I ran into this problem a few times myself, and this indeed has to do with zipping the folder instead of just the contents like you're supposed to.

For those working from the terminal...

While INSIDE of the directory where the .js files are sitting, run the following:

zip -r ../zipname.zip *

The * is instructing the client to zip all the contents within this folder, ../zipname.zip is telling it to name the file zipname.zip and place it right outside of this current directory.

3 Comments

Upvoted for providing clear instructions on the correct way to zip the files.
By default, the handler for nodejs applications is expected to be in a file called "index.js"
This solved the problem for me after experiencing a lot of trouble packaging my Lambda function.
2

I had the same problem sometime ago - I reformatted the code.

function lambdafunc1(event, context) {
...
...
...
}

exports.handler = lambdafunc1

Comments

2

The problem occurs when the handler cannot be located in the zip at first level. So anytime you see such error make sure that the file is at the first level in the exploded folder.

To fix this zip the files and not the folder that has the files.

Comments

1

I hit this snag yesterday. After much pain, trial and error, I found the culprit. Apparently after a certain version of Node.js on AWS, they name the index file index.mjs - the extension is now .mjs (correct) and not simply .js (wrong).

The funny part is I actually noticed the difference, but unwittingly was packaging an index file with extension .jsm, and not the correct one .mjs. It took me 3 hours to figure out why it was not working, but that exposed the problem. The instructions in the AWS official documentation show how to package a .js file and allude to packaging an index file without dependencies on a blog under the AWS umbrella that is also using the old .js extension - apparently no one noticed for a while.

Anyhow, best of luck, and I hope you never have to deal with this again.

Comments

0

Correct Lambda function declaration can look like this:

var func = function(event, context) {
   ...
};

exports.handler = func;

You may have other syntax errors that prevent the index.js file from being properly ran. Try running your code locally using another file and using the index.js as your own module.

2 Comments

Did not work for me, I have other lambda functions working without this change so I am not sure this is the issue..
You need to provide more information. What you've described can either be something very trivial or some other error.
0

make sure in your handler following code added

exports.handler =  (event, context, callback) => {
...
}

Comments

0

Another reason this can occur is if you don't do an npm install in the folder before packaging and deploying.

Comments

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.