41

I'm trying to use the Serverless Framework to create a Lambda function that uses open weather NPM module. However, I'm getting the following exception, but my node_modules contain the specific library.

I have managed to run the sample, (https://github.com/serverless/examples/tree/master/aws-node-rest-api-with-dynamodb) successfully, now hacking to add node module to integrate open weather API.

Endpoint response body before transformations: {"errorMessage":"Cannot find module 'Openweather-Node'","errorType":"Error","stackTrace":["Module.require (module.js:353:17)","require (internal/module.js:12:17)","Object.<anonymous> (/var/task/todos/weather.js:4:17)","Module._compile (module.js:409:26)","Object.Module._extensions..js

My code

'use strict';

  const AWS = require('aws-sdk'); // eslint-disable-line import/no-extraneous-dependencies
  var weather = require('Openweather-Node');

  const dynamoDb = new AWS.DynamoDB.DocumentClient();

  module.exports.weather = (event, context, callback) => {
    const params = {
      TableName: process.env.DYNAMODB_TABLE,
      Key: {
        id: event.pathParameters.id,
      },
    };

    weather.setAPPID("mykey");
    //set the culture
    weather.setCulture("fr");
    //set the forecast type
    weather.setForecastType("daily");

    const response = {
      statusCode: 200,
      body: "{test response}",
    };
    callback(null, response);          
  };
Arafat Nalkhande
  • 11,078
  • 9
  • 39
  • 63
Charith De Silva
  • 3,650
  • 4
  • 43
  • 47

13 Answers13

47

Did you npm install in your working directory before doing your serverless deploy? The aws-sdk node module is available to all lambda functions, but for all other node dependencies you must install them so they will be packaged with your lambda when you deploy.

You may find this issue on the serverless repository helpful (https://github.com/serverless/serverless/issues/948).

marcusmolchany
  • 661
  • 6
  • 5
10

I fixed this error when in package.json I moved everything from devDependencies to dependencies.

Cheers

user1398619
  • 696
  • 7
  • 8
3

I was doing some stupidity. But still wanted to put here so any beginner like me should not struggle for it. I copied the serverless.xml from example where handler value was

handler: index.handler

But my index.js was in src folder. Hence I was getting file not found. It worked after I change the handler value to

 handler: src/index.handler
skvp
  • 1,940
  • 1
  • 20
  • 25
2

I don't if it applies to this answer but in case someone just needs a brain refresh I forgot to export my handler and was exporting the file with was looking for a default export that didn't exist...

changed from this... handler: foldername/exports

to this... handler: foldername/exports.handler

Max Phillips
  • 6,991
  • 9
  • 44
  • 71
1

I have the same problem with serverless framework to deploy multiple lambda functions. I fixed by the following steps

  1. Whatever you keep the path at the handler like handler: foldername/exports.handler
  2. Name the file inside the folder as exports.js(whatever you name the handler)
  3. run serverless deploy

This should solve your problem

Ravikiran Reddy Kotapati
  • 2,485
  • 3
  • 22
  • 27
1

I went to cloud watch and looked for the missing packages

Then npm i "missing package" and do sls deploy

The missing packages are needed in the dependencies in my case there was some on the devDepencies and another missing

Nemesius
  • 179
  • 10
0

You need to do the package deployment in case you have external dependencies. Please see this answer

AWS Node JS with Request

Reference

http://docs.aws.amazon.com/lambda/latest/dg/nodejs-create-deployment-pkg.html

Community
  • 1
  • 1
bibek shrestha
  • 448
  • 3
  • 9
0

In several cases, don't forget to check global serverless installation. mine solved by reinstalling:

npm install -g serverless
Jastria Rahmat
  • 776
  • 1
  • 6
  • 27
0

In my case, what worked was switching to node 10 (via nvm). I was using a newer version of node (v15.14.0) than was probably supported by the packages.

AndyFaizan
  • 1,833
  • 21
  • 30
0

My case was configuring params for creating an AWS lambda function. The right string for a handler was (last row):

Resources:
  StringResourceName:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: myFileName.handler

Where myFileName is the name of a file that I use as the zip file.

Roman
  • 19,236
  • 15
  • 93
  • 97
0

You have issue with your ts files, as serverless-offline plugin cannot find equivalent js file it throws error module not found.

There is a work around to it to install (serverless-plugin-typescript) . Only issue with the plugin is that it creates a new .build/.dist folder with transpiled js files

-1

For anyone developing python lambda functions with serverless-offline and using a virtual environment during local development, deactivate your environment, delete it entirely, and recreate it. Install all python requirements and try again. This worked for me.

Rai
  • 394
  • 5
  • 23
-2

For me, the issue was that the handler file name contained a dot.

main-handler.graphql.js caused serverless "Error: Cannot find module 'main'.

when I changed the file to main-handler-graphql.js everything worked.

Eitan Frailich
  • 132
  • 1
  • 6