我正在尝试使用无服务器框架创建一个使用开放天气 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 install
? aws-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/