Saturday 18 March 2017

Serverless framework with aws lambda error "Cannot find module"

I'm trying to use the serverless framework to create lambda function that uses open weather npm module. However, I'm getting the following exception, but my node_modules contain a specific library. I'm quite new to serverless and node. 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. Thanks in advance.

 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);

    // weather.forecast({method: 'cityID', lang: 'fr', units: 'metric'}, function(err, data) {
    //   if (!err) {
    //     console.log(data);
    //     const response = {
    //       statusCode: 200,
    //       body: data,
    //     };
    //     callback(null, response);
    //
    //   } else {
    //     console.error(err.message);
    //   }
    //
    // });


    // fetch todo from the database
    // dynamoDb.get(params, (error, result) => {
    //   // handle potential errors
    //   if (error) {
    //     console.error(error);
    //     callback(new Error('Couldn\'t fetch the todo item.'));
    //     return;
    //   }
    //
    //   // create a response
    //   // const response = {
    //   //   statusCode: 200,
    //   //   body: JSON.stringify(result.Item),
    //   // };
    //   // callback(null, response);
    // });
  };



via Charith

No comments:

Post a Comment