AWS Lambda - 邮编问题 - "errorMessage": "Cannot find module './dist/commonjs/index.js'",

AWS Lambda - Zip Issue- "errorMessage": "Cannot find module './dist/commonjs/index.js'",

一个错误是:

"errorMessage": "Cannot find module './dist/commonjs/index.js'",

另一个错误是:

Unable to import module 'index': Error
    at Function.Module._resolveFilename (module.js:469:15)

我看过其他关于第二个错误的帖子,但我已经按照他们的建议做了一切,但仍然卡住了。而且他们没有提到 /dist/commonjs/index.js 问题。

我的 Lambda 函数处理程序设置为 "index.handler",我的主要代码在 index.js.

我在 Windows。我的 zip 文件 (HebrewVocab.zip) 看起来像这样。 而 node_modules 文件夹似乎是完整的,看起来像这样: 我压缩并上传了我写的 PowerShell:

Compress-Archive -Path index.js,package.json,node_modules -DestinationPath $ZipFileName 
aws lambda update-function-code --function-name HebrewVocab --zip-file 
fileb://HebrewVocab.zip

我的 index.js 以此代码开头:

exports.handler = function(event, context, callback) {
    var alexa = Alexa.handler(event, context, callback);
    alexa.registerHandlers(handlers); 
    alexa.execute();
};

所以我似乎满足了我在所有其他类似错误的帖子上阅读的所有要求。我之前有一个 HelloWorld 项目在工作,但它没有使用 alexa-sdk。

我也按照 Whosebug 上的一个人的建议做了 "npm install npm"?有必要吗?

当我"test"在Lamba时,对比使用Alexa语音进行测试,我看到了细节:

"errorMessage": "Cannot find module './dist/commonjs/index.js'",

./dist/commonjs 应该是标准路径前缀吗?我什至将 "function" 导出到我的硬盘驱动器(作为 .zip),这正是我上传的内容),并且在 .zip 文件的根目录中肯定有一个 index.js。

作为一个疯狂的猜测,我发现 commonjs 是一个包,所以我做了一个 "npm install commonjs --save",重新压缩并重新上传,但结果完全相同。

我后来发现有一个 /node_modules/i18next/dist/commonjs 和一个 index.js,但不确定这是否相关。我什至尝试将 dist/commonjs 复制到我的根目录和 node_modules 下,但仍然出现同样的错误。

当我 运行 在本地使用时 运行 没问题:

lambda-local -l index.js -h handler -e eventHelloWorld.json

错误信息截图:

最后但同样重要的是,我的 package.json 看起来像这样:

{
  "name": "HebrewVocab",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "alexa-sdk": "^1.0.18",
    "commonjs": "0.0.1",
    "npm": "^5.5.1"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

我还能尝试什么?我在 Amazon 支持论坛上发帖,但没有答案,所以我的项目一直停滞不前,直到我弄清楚这个问题。

2017 年 7 月 11 日更新:

好吧,我只是做了一个疯狂的观察。由上述 Powershell 创建的 .zip 创建的 .zip 大小不同于 Windows 右键单击​​ "Send to" 压缩文件夹。如果我使用 Total Command "Synchronize Directories" 比较这两个 zip,所有文件和结构的内容都是相同的。然而,从手动 Windows zip 创建的那个有效,而由 Powershell 创建的那个给出了最初在此线程中报告的错误。 Powershell 创建的数据略小,分别为 5,019,123 和 5,032,770。我尝试了一些其他的 powershell 压缩选项,但它们创建了更大的文件(关闭它产生了大约 30 MB 的文件)。

我已经进入模糊地带 - 有什么想法吗?

我 运行 使用 PowerShell 脚本压缩和上传 lambda 函数的代码,以供 Alexa 技能调用。根据 PowerShell github 存储库中的 issue #2140Compress-Archive 生成的存档与 OS X 不兼容,我怀疑任何派生的 Unix OS。

我最终使用 bash -c 'zip -r filename *' 提到的 here 因为我已经为 windows 安装了 linux 子系统。我最初尝试了 Compress-7Zip(也提到过),但是它非常慢,以至于我认为它在我尝试的前几次就挂了。

对于 Windows,我最终使用 7-zip 如下(在 Powershell 文件中,因此 $ZipFileName 是在前一行定义的变量)。

& "c:\Program Files-Zipz.exe" a -r $ZipFileName index.js package.json client_secret.json GoogleCalendarAPI.js node_modules .credentials calendar-nodejs-quickstart.json

您可以像这样压缩旧学校的方式:

function ZipFiles( $zipfilename, $sourcedir )
{
   Add-Type -Assembly System.IO.Compression.FileSystem
   $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
   [System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir,
        $zipfilename, $compressionLevel, $false)
}

$zipdest = (Get-Item .\ | Select-Object -ExpandProperty FullName) + '\lambda.zip'
$zippath = (Get-Item .\lambda | Select-Object -ExpandProperty FullName)
ZipFiles $zipdest $zippath