简体   繁体   English

带有 AWS Lambda 错误“找不到模块”的无服务器框架

[英]Serverless Framework with AWS Lambda error "Cannot find module"

I'm trying to use the Serverless Framework to create a Lambda function that uses open weather NPM module.我正在尝试使用无服务器框架创建一个 Lambda function,它使用开放天气 NPM 模块。 However, I'm getting the following exception, but my node_modules contain the specific library.但是,我收到以下异常,但我的 node_modules 包含特定的库。

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

Did you npm install in your working directory before doing your serverless deploy ?在进行serverless deploy之前,您是否在工作目录中npm install 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. aws-sdk节点模块可用于所有 lambda 函数,但对于所有其他节点依赖项,您必须安装它们,以便在部署时将它们与您的 lambda 打包。

You may find this issue on the serverless repository helpful ( https://github.com/serverless/serverless/issues/948 ).您可能会在无服务器存储库中发现此问题很有帮助 ( https://github.com/serverless/serverless/issues/948 )。

I fixed this error when in package.json I moved everything from devDependencies to dependencies .我在package.json中将所有内容从devDependencies移动到dependencies时修复了这个错误。

Cheers干杯

I don't if it applies to this answer but in case someone just needs a brain refresh I forgot to export my handler and was exporting the file with was looking for a default export that didn't exist...如果它适用于这个答案,我不知道,但如果有人只需要大脑刷新,我忘记导出我的处理程序并且正在导出文件,正在寻找一个不存在的默认导出......

changed from this... handler: foldername/exports从此更改... handler: foldername/exports

to this... handler: foldername/exports.handler到这个... handler: foldername/exports.handler

I have the same problem with serverless framework to deploy multiple lambda functions.我对部署多个 lambda 函数的无服务器框架有同样的问题。 I fixed by the following steps我通过以下步骤修复

  1. Whatever you keep the path at the handler like handler: foldername/exports.handler无论您在处理程序中保留路径,例如handler: foldername/exports.handler
  2. Name the file inside the folder as exports.js(whatever you name the handler)将文件夹内的文件命名为exports.js(无论您如何命名处理程序)
  3. run serverless deploy运行serverless deploy

This should solve your problem这应该可以解决您的问题

You need to do the package deployment in case you have external dependencies.如果您有外部依赖项,则需要进行包部署。 Please see this answer请看这个答案

AWS Node JS with Request 带有请求的 AWS 节点 JS

Reference参考

http://docs.aws.amazon.com/lambda/latest/dg/nodejs-create-deployment-pkg.html http://docs.aws.amazon.com/lambda/latest/dg/nodejs-create-deployment-pkg.html

你应该安装你的模块如下:

npm install -D <your-module-name> 

In several cases, don't forget to check global serverless installation.在某些情况下,不要忘记检查全局无服务器安装。 mine solved by reinstalling:我通过重新安装解决了:

npm install -g serverless

In my case, what worked was switching to node 10 (via nvm).就我而言,有效的是切换到节点 10(通过 nvm)。 I was using a newer version of node (v15.14.0) than was probably supported by the packages.我使用的节点版本(v15.14.0)比软件包可能支持的更新版本。

My case was configuring params for creating an AWS lambda function.我的案例是为创建 AWS lambda 函数配置参数。 The right string for a handler was (last row):处理程序的正确字符串是(最后一行):

Resources:
  StringResourceName:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: myFileName.handler

Where myFileName is the name of a file that I use as the zip file.其中myFileName是我用作 zip 文件的文件的名称。

You have issue with your ts files, as serverless-offline plugin cannot find equivalent js file it throws error module not found.你的 ts 文件有问题,因为 serverless-offline 插件找不到等效的 js 文件,它会抛出找不到模块的错误。

There is a work around to it to install ( serverless-plugin-typescript ) .有一个解决方法可以安装( serverless-plugin-typescript )。 Only issue with the plugin is that it creates a new .build/.dist folder with transpiled js files该插件的唯一问题是它创建了一个新的 .build/.dist 文件夹,其中包含转译的 js 文件

I was doing some stupidity.我在做一些愚蠢的事情。 But still wanted to put here so any beginner like me should not struggle for it.但仍然想放在这里,所以像我这样的初学者不应该为它而奋斗。 I copied the serverless.xml from example where handler value was我从处理程序值为的示例中复制了 serverless.xml

handler: index.handler

But my index.js was in src folder.但我的 index.js 在 src 文件夹中。 Hence I was getting file not found.因此我找不到文件。 It worked after I change the handler value to在我将处理程序值更改为

 handler: src/index.handler

For anyone developing python lambda functions with serverless-offline and using a virtual environment during local development, deactivate your environment, delete it entirely, and recreate it.对于任何开发 python lambda 功能并在本地开发期间使用serverless-offline和虚拟环境的人,请停用您的环境,将其完全删除,然后重新创建它。 Install all python requirements and try again.安装所有 python 要求,然后重试。 This worked for me.这对我有用。

I went to cloud watch and looked for the missing packages我去cloud watch找丢失的包裹

Then npm i "missing package" and do sls deploy然后npm i "missing package"并执行sls deploy

The missing packages are needed in the dependencies in my case there was some on the devDepencies and another missing在我的情况下,依赖项中需要缺少的包

Got the same Problem.遇到了同样的问题。 Used the serverless node template and did not do an npm init afterwards.使用了无服务器节点模板,之后没有做npm init

For me, the issue was that the handler file name contained a dot.对我来说,问题是处理程序文件名包含一个点。

main-handler.graphql.js caused serverless "Error: Cannot find module 'main'. main-handler.graphql.js导致serverless "Error: Cannot find module 'main'.

when I changed the file to main-handler-graphql.js everything worked.当我将文件更改为main-handler-graphql.js一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS Lambda + Angular web 应用抛出“错误:找不到模块‘@vendia/serverless-express’” - AWS Lambda + Angular web app throwing "Error: Cannot find module '@vendia/serverless-express'" 使用无服务器框架将 Python package 部署到 AWS lambda 时出错 - Error deploying Python package to AWS lambda using Serverless framework 无服务器 AWS Lambda CORS 错误 - Serverless AWS Lambda CORS Error 错误:在 NodeJS AWS 中找不到模块“aws-sdk” Lambda Function - Error: Cannot find module 'aws-sdk' in NodeJS AWS Lambda Function AWS Lambda function on Java with Serverless framework and GraalVM - AWS Lambda function on Java with Serverless framework and GraalVM 使用无服务器框架在 lambda AWS 中授予授权代码 - Authorization code grant in lambda AWS with Serverless Framework 在 Lambda 中找不到模块“aws-cloudfront-sign” - Cannot find module 'aws-cloudfront-sign' in Lambda AWS lambda 无服务器中不存在此类文件错误 - AWS lambda no such file exists error in serverless 如何从 AWS Lambda 函数 + 无服务器框架的 URL 中删除阶段? - How to remove stage from URLs for AWS Lambda functions + Serverless framework? 使用无服务器框架的 Lambda Snapstart - Lambda Snapstart with Serverless framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM