带有AWS Lambda错误的“无服务器框架"“无法找到模块"; [英] Serverless Framework with AWS Lambda error "Cannot find module"

查看:121
本文介绍了带有AWS Lambda错误的“无服务器框架"“无法找到模块";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用无服务器框架来创建使用开放天气NPM模块的Lambda函数.但是,出现以下异常,但是我的node_modules包含特定的库.

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.

我设法运行了示例,( https://github.com/serverless/examples/tree/master/aws-node-rest-api-with-dynamodb ),现在正在黑客添加节点模块以集成开放天气API.

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

我的代码

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

推荐答案

在执行serverless deploy之前,您是否在工作目录中已经npm install? aws-sdk节点模块可用于所有lambda函数,但是对于所有其他节点依赖项,您必须安装它们,以便在部署时将它们与lambda打包在一起.

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.

您可能会发现无服务器存储库上的此问题很有帮助( https://github.com/serverless/serverless/issues/948 ).

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

这篇关于带有AWS Lambda错误的“无服务器框架"“无法找到模块";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆