Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to import module 'handler' aws lambda function in nodejs

I keep getting this error and I don't know what is causing it.

I am having an API that based on a condition will post to another API, but I get this error in my wrapping API.

Here is the code ...

handler.js

'use strict';
const axios = require('axios');

module.exports.thumbnailWrapperAPI = (event, context, callback) => {


  const incomingData = JSON.parse(event.body);
  if(incomingData.source.includes('png') || incomingData.source.includes('jpg')){
    const newLocal = 'some endpoint...';
    // call image resizing API...
    axios.post(newLocal,{
      source: incomingData.source,
      target: incomingData.target,
      width: incomingData.width
    })
    .then(response => callback(null,response))
    .catch(error => callback(error))

  } else if(incomingData.source.includes('html')) {
    // handle HTML
  } else {
    //...
  };
};

serverless.yaml

service: thumbnailWrapperAPI 
provider:
  name: aws
  runtime: nodejs8.10
  region: eu-west-1

functions:
  thumbnailWrapperAPI:
    handler: handler.thumbnailWrapperAPI
    events:
      - http:
          path: generatethumbnail/
          method: post
          cors: true

Any advice would be appreciated.

ERROR MESSAGE:

Unable to import module 'handler': Error
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.Module._load (module.js:474:25)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/var/task/handler.js:2:15)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
like image 630
squeekyDave Avatar asked Sep 03 '18 09:09

squeekyDave


People also ask

How do I resolve the unable to import module error that I receive when I run Lambda code in Python?

To resolve this error, create a deployment package or Lambda layer that includes the libraries that you want to use in your Python code for Lambda. Important: Make sure that you put the library that you import for Python inside the /python folder.

How do I specify a handler in AWS Lambda?

This function handler name reflects the function name ( lambda_handler ) and the file where the handler code is stored ( lambda_function.py ). To change the function handler name in the Lambda console, on the Runtime settings pane, choose Edit.


1 Answers

The error message doesn't help too much, but I found out that this message is often caused by a lost npm package. If you test the lambda in the AWS console, you can see the specific details.

like image 145
aarkerio Avatar answered Nov 12 '22 09:11

aarkerio