Serverless-express: unable to import module "lambda"

Created on 20 Mar 2017  Â·  19Comments  Â·  Source: vendia/serverless-express

I'm trying to migrate an existing express app.

I've deployed, but I'm getting this error in Cloudwatch:

Unable to import module 'lambda': Error
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)

And "internal server error" when trying to hit my ApiUrl.

I'm requireing my server in lambda.js and changed "main": in my package.json to lambda.js as well as following the other instructions. Could I be missing something obvious?

Most helpful comment

It seems that this is a generic error which hides the real issue.
The most consistent way I've found to debug is via the API Gateway test console which actually reveals the root error messages.

All 19 comments

Where do you see this error? Is it on your Lambda function or on API Gateway side?

@zaklampert could you post your lambda.js? It sounds like it's unable to find a file you're trying to require. I would suggest adding some console.logs in your lambda.js to find out more.

having the very same issue using the example app and following the instructions

Please provide more details such as OS, AWS CLI Version, does it with
locally for you (npm run local)?

On Fri, Apr 7, 2017, 10:02 AM Pascal Brogle notifications@github.com
wrote:

having the very same issue using the example app and following the
instructions

—
You are receiving this because you commented.

Reply to this email directly, view it on GitHub
https://github.com/awslabs/aws-serverless-express/issues/53#issuecomment-292593296,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABy6lxqFTloV9z68WnNLkXaC_lSK0iqYks5rtmwPgaJpZM4Mi5T7
.

I have the same issue. I am using a hybrid aws-serverless-express build where webpack dumps the output to a separate build path. I've updated the CodeUri field in cloudformation.yaml to point to the new build directory. When deployed, I can see that the code in the lambda editor matches what is in the build/lambda.js file i have locally and also downloading and un-archiving the file that is uploaded to S3 I see the lambda.js file as well as other assets. The webpacked lambda.js also runs just fine with npm run local so I'm not sure what is exactly going wrong on the lambda side. I'm also running node6.10 if that makes any difference and using mac os x locally.

Can you confirm via Lambda console that the handler is configured
correctly ("lambda.handler") and that the code in the console does export a
"handler" function. Can you also list the files in your build directory and
confirm which version of node you are running locally (recommend matching
the version used on Lambda 6.10).

On Tue, Apr 11, 2017, 12:53 AM Wing Lian notifications@github.com wrote:

I have the same issue. I am using a hybrid aws-serverless-express build
where webpack dumps the output to a separate build path. I've updated the
CodeUri field in cloudformation.yaml to point to the new build directory.
When deployed, I can see that the code in the lambda editor matches what is
in the build/lambda.js file i have locally and also downloading and
un-archiving the file that is uploaded to S3 I see the lambda.js file as
well as other assets. The webpacked lambda.js also runs just fine with npm
run local so I'm not sure what is exactly going wrong on the lambda side.
I'm also running node6.10 if that makes any difference and using mac os x
locally.

—
You are receiving this because you commented.

Reply to this email directly, view it on GitHub
https://github.com/awslabs/aws-serverless-express/issues/53#issuecomment-293180084,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABy6l-xDuVmHL5hiqVeNEZV-i62AJKTtks5ruzGJgaJpZM4Mi5T7
.

I did download the created package and could not run it locally. When running it locally proper error got displayed that hinted at the problem. The problems were the missing dependencies (for some reasons npm install did only install top level dependencies, but not others). After moving the examples directory to its own directory, it worked.

@zaklampert are you still having this issue? This usually indicates that the file does not exist in the s3 zip. Check your function configuration (first part of the handler should be the file name, e.g. index.handler, where index is index.js and it exports a handler function).

@winglian if the code is displayed inline, try changing your lambda function handler to index.handler.

I think this stems from how the function is being zipped up. I've seen in the past where the whole folder will get zipped instead of the contents of the folder-- lambda freaks out a little about that.

I would suggest making sure that you're zipping up the contents of the folder and re-upload that file.

It seems that this is a generic error which hides the real issue.
The most consistent way I've found to debug is via the API Gateway test console which actually reveals the root error messages.

in case you are using serverless framework to deploy , ensure you do npm install of all your packages before deploying the lamda function.

I ran into this issue. If you started with a serverless template, you need to change _handler.js_ to _lambda.js_ and then change the file reference in your package.json for main to reflect that change.

"npm run local" is working correct but getting --unable to import module "lambda" error when testing lambda function or calling api gateway.

This is a generic error that is not specific to this project.

I am getting this error. Any help is appreciated.

@suhas92

Couple of things to look at when this happens:
1) Does the handler name actually match the Filename.handler format (e.g. index.handler or MyLambda.doSomeStuff) which expects index and MyLambda files to contain handler and doSomeStuff respectively
2) Test the lambda with a test event and _first_ look at the details tab of the lambda to see if there's anything revealing (e.g. Unable to import module: module_name can't import module missing_module.py) which may indicate that you forgot a file in the zip or had some other packaging error.
3) Look at the cloudwatch logs

Hope this helps.

It seems that this is a generic error which hides the real issue.
The most consistent way I've found to debug is via the API Gateway test console which actually reveals the root error messages.
Running into this issue with a node project packaged up with sam package and deployed using sam deploy.

@mcwebb Could you elaborate on how you see the real error message in the API gateway test console - - I'm not familiar with that console - and how to locate the lambda related output within it?

Thanks,
Faraz

I came across this issue and turns out it's a CASE SENSITIVE issue

  1. Using TypeScript so my lambda function is insideLambda.ts which is transpiled to Lambda.js (note the L)
  2. template.yaml defines the handler as lambda.handler

This works just fine in with sam local start-api, but generate that exact error message when running on real environment. Correct to the right case works for me.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

theweaklink picture theweaklink  Â·  3Comments

donny08 picture donny08  Â·  8Comments

muswain picture muswain  Â·  8Comments

denneulin picture denneulin  Â·  8Comments

sclark39 picture sclark39  Â·  7Comments