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

squeekyDave picture squeekyDave · Sep 3, 2018 · Viewed 7.3k times · Source

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)

Answer

aarkerio picture aarkerio · Mar 12, 2019

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.