node.js - 带有 AWS Lambda 错误 "Cannot find module"的无服务器框架

标签 node.js amazon-web-services aws-lambda aws-sdk serverless-framework

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

我成功地运行了示例,( https://github.com/serverless/examples/tree/master/aws-node-rest-api-with-dynamodb ),现在开始添加 Node 模块以集成开放天气 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);          
  };

最佳答案

在执行无服务器部署之前,您是否在工作目录中npm installaws-sdk Node 模块可用于所有 lambda 函数,但对于所有其他 Node 依赖项,您必须安装它们,以便在您部署时将它们与您的 lambda 打包在一起。

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

关于node.js - 带有 AWS Lambda 错误 "Cannot find module"的无服务器框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42880987/

相关文章:

node.js - AngularJS 和 ExpressJS 路由问题?

amazon-web-services - 使用 AWS CLI 启动 CloudFormation 模板时的可选参数

amazon-web-services - 使用 SAM 应用程序如何定义 body 映射模板

java - 使用 Lambda 设置 DynamoDB 触发器

ios - 我如何通知客户端以便某个帖子不再存在?

node.js - Twilio 客户端错误 : Twilio IP messaging with AWS Lambda/AWS API Gateway

node.js - react 样板 npm 安装错误

python - 在 Python 中将文件上传到 AWS S3 存储桶文件夹导致正则表达式错误

amazon-web-services - 查看 Amazon S3 上的当前请求数和数据传输

django - http 403 错误 + "readv() failed (104: Connection reset by peer) while reading upstream"