squeekyDave
squeekyDave

Reputation: 972

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)

Upvotes: 6

Views: 14003

Answers (4)

Andreas Forsl&#246;w
Andreas Forsl&#246;w

Reputation: 2738

In my case, it turned out that I used both python and nodejs for my lambdas, but hadn't set the nodejs lambda function's runtime environment as nodejs. Previously, my serverless.yml looked similar to this:

provider:
  name: aws
  runtime: python3.7
  stage: dev
  region: eu-west-1 
  profile: my-profile

functions:
  nodejs-func:
    handler: nodejs_func.handler
    events:
      - websocket:
          route: nodejs-func
  python-func:
    handler: python_func.handler
    events:
      - websocket:
          route: python-func

Simply give the runtime environment for the nodejs lambda in order to solve this:

provider:
  name: aws
  runtime: python3.7
  stage: dev
  region: eu-west-1 
  profile: my-profile

functions:
  nodejs-func:
    handler: nodejs_func.handler
    runtime: nodejs10.x # Provide the runtime environment for lambda func
    events:
      - websocket:
          route: nodejs-func
  python-func:
    handler: python_func.handler
    events:
      - websocket:
          route: python-func

Upvotes: 2

The Oracle
The Oracle

Reputation: 2503

You will also get this error when you require a module or file using a wrong path. In other words requiring a module/file that does not exist.

It could be a custom module or npm.

Please double check all your module import paths and see that they are accurate.

Upvotes: 2

aarkerio
aarkerio

Reputation: 2364

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.

Upvotes: 8

squeekyDave
squeekyDave

Reputation: 972

Ok I solved it by removing my package.json then adding it again and installing NOT as dev dependencies my packages and it worked.

Upvotes: 3

Related Questions