Javascript aws-lambda 找不到模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30678348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
aws-lambda Cannot find module
提问by Daniel Munoz
I keep getting this error in the aws-lambda console when uploading code from a zip file. I have tried uploading other zip files and they work correctly. The .js file is named "CreateThumbnail.js" in the zip file. I believe the handler is also named properly "CreateThumbnail.handler". the node_modules subdirectory is also setup. Anyone have any idea?
从 zip 文件上传代码时,我一直在 aws-lambda 控制台中收到此错误。我尝试上传其他 zip 文件,它们工作正常。.js 文件在 zip 文件中名为“CreateThumbnail.js”。我相信处理程序也被正确命名为“CreateThumbnail.handler”。node_modules 子目录也已设置。任何人有任何想法?
{
"errorMessage": "Cannot find module 'CreateThumbnail'",
"errorType": "Error",
"stackTrace": [
"Function.Module._resolveFilename (module.js:338:15)",
"Function.Module._load (module.js:280:25)",
"Module.require (module.js:364:17)",
"require (module.js:380:17)"
]
}
回答by Aaron
The way I was able to get this to work was:
我能够让它发挥作用的方式是:
- Name the file exports.js
- Name the handler, within the file, exports.handler
- Set the handler in the lambda config to exports.handler
- Zip up only the contents of the folder, not the folder itself (as mentioned above) and rename the zip file exports.zip
- 命名文件exports.js
- 在文件中将处理程序命名为exports.handler
- 将 lambda 配置中的处理程序设置为exports.handler
- 仅压缩文件夹的内容,而不是文件夹本身(如上所述)并重命名 zip 文件exports.zip
回答by Brij Raj Singh - MSFT
Ok, I did this myself, just make sure that you make the zip such that the .js file doesn't end up inside a folder, because AWS would unzip the file you upload and tries to find a .js file by the name of handler you gave, and if its inside a folder it won't help you.
好的,我自己做了这个,只要确保你制作了 zip 文件,这样 .js 文件就不会出现在文件夹中,因为 AWS 会解压缩你上传的文件并尝试通过名称查找 .js 文件你给的处理程序,如果它在一个文件夹内,它不会帮助你。
回答by Catalin Ciurea
This exact error can show up if your zipped file(s) do not have world-wide read permission. (chmod -R ugo+r).
如果您的压缩文件没有全球读取权限,则可能会出现此确切错误。( chmod -R ugo+r).
Check the file permissions before they are zipped. This is not emphasized enaugh unfortunately by AWS and it caused a lot of headaches for many.
在压缩文件之前检查文件权限。不幸的是,AWS 并没有充分强调这一点,它给许多人带来了很多麻烦。
回答by Roman Starkov
One possible problem is if you upload the lambda as a zip file created via PowerShell Compress-Archive. Compress-Archivehas a bugwhich causes AWS to extract the files into a flat tree (no subdirectories), with backslashes in filenames:
一个可能的问题是,如果您将 lambda 作为通过 PowerShell 创建的 zip 文件上传Compress-Archive。Compress-Archive有一个错误导致 AWS 将文件提取到一个平面树(没有子目录)中,文件名中带有反斜杠:
回答by valdeci
If you are using AWS Lambda Layersyou need to validate if your directory structure is on the needed structure for a layer:
如果您使用AWS Lambda 层,则需要验证您的目录结构是否位于层所需的结构上:
For example for the moment.jsnode.js module you need the following structure:
例如,对于moment.jsnode.js 模块,您需要以下结构:
aws-lambda-layer.zip
│ nodejs
│ nodejs/node_modules
└ nodejs/node_modules/moment
So to create a layer zip file with the correct structure we can use the following command on the root of our project:
因此,要创建具有正确结构的层 zip 文件,我们可以在项目的根目录上使用以下命令:
mkdir -p nodejs && cp -r node_modules nodejs/ && zip -r aws-lambda-layer.zip nodejs
回答by LeOn - Han Li
Some library files might not have global Read so lambda will not be able to read to content and build the content.
某些库文件可能没有全局读取,因此 lambda 将无法读取内容并构建内容。
Make sure all files in node_modulesare readable before packaging:
node_modules打包前确保所有文件都可读:
chmod -R +r node_modules
Then zip and upload.
然后压缩并上传。
回答by DonPedro
I had this problem on a custom module I had built that was in the node_modules dir. Everything ran fine in testing on my Win10 machine, but when uploaded I kept getting that same "Cannot find module 'modulename'" error.
我在 node_modules 目录中构建的自定义模块上遇到了这个问题。在我的 Win10 机器上测试时一切正常,但上传时我一直收到同样的“找不到模块‘模块名’”错误。
It turns out that I had a mismatch; here's the package.json line from the module that couldn't be found:
原来我有一个不匹配;这是无法找到的模块中的 package.json 行:
"main": "./build/modulename.js",
and here's the actual filename:
这是实际的文件名:
Modulename.js
Case-sensitive; Windows isn't, linux (and thus AWS) is.
区分大小写; Windows 不是,Linux(因此 AWS)是。
回答by Fábio Santos
This is unrelated but google brought me here, so:
这是无关的,但谷歌把我带到这里,所以:
AWS will give you an error:
AWS 会给你一个错误:
Unable to import module '<myfile>': Error
What was really happening for me, was that was requiring an unexisting JS file. The error is a bit misleading.
对我来说真正发生的事情是需要一个不存在的 JS 文件。这个错误有点误导。
回答by Esdras Mayrink
AWS Lambda uses the name of the file and the name of the handler function, so if you defined your handler like this: exports.myHandler = function(event, context)in a file named index.js, your handler is index.myHandler.
AWS Lambda 使用文件名和处理程序函数的名称,因此如果您这样定义处理程序:exports.myHandler = function(event, context)在名为 的文件中index.js,您的处理程序是index.myHandler.
回答by CarlPWilliams
This turned out to be a simple one for me.
结果证明这对我来说很简单。
I was getting, cannot create index. in my case, my main lambda file with the exports.handler in had to be called index.js
我得到了,无法创建索引。在我的情况下,我的带有exports.handler的主要lambda文件必须被称为index.js
Try calling your main file CreateThumbnail.js
尝试调用您的主文件 CreateThumbnail.js


