问题
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);
};
回答1:
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).
回答2:
I fixed this error when in package.json
I moved everything from devDependencies
to dependencies
.
Cheers
回答3:
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
回答4:
Got the same Problem. Used the serverless node template and did not do an
npm init
afterwards.
回答5:
I have the same problem with serverless framework to deploy multiple lambda functions. I fixed by the following steps
- Whatever you keep the path at the handler like
handler: foldername/exports.handler
- Name the file inside the folder as exports.js(whatever you name the handler)
- run
serverless deploy
This should solve your problem
来源:https://stackoverflow.com/questions/42880987/serverless-framework-with-aws-lambda-error-cannot-find-module