簡體   English   中英

構建基本 Web 應用程序教程中的 AWS Lambda“找不到模塊 aws-sdk”

[英]AWS Lambda "cannot find module aws-sdk" in Build a Basic Web Application tutorial

我試圖通過從頭開始運行Build a Basic Web Application教程來開始使用 AWS。 我想我可以一步一步地跟着它做,並有一個簡單的 Hello World 網頁前端到一個小數據庫。 但是我遇到了一個奇怪的錯誤:包括准系統,默認aws-sdk模塊失敗了!

精簡版:

這段代碼:

// Include the AWS SDK module
const AWS = require('aws-sdk');

生成此錯誤: Error: Cannot find module 'aws-sdk'

很長的故事:

我正在逐步遵循該過程,使用 AWS 在線工具創建所有部分。 當我創建 Lambda 函數時,第一個問題出現了。 我並不太擔心 Create Function 工具顯示的是 Node.js 版本 18.x 而不是教程的 Node.js 16.x,但我確實注意到它生成的 Lambda shell 被命名為index.mjs而不是index.js 沒問題,我會使用那個文件。 我用 Amazon 的 HelloWorld 代碼替換了內容。

// Define handler function, the entry point to our code for the Lambda service
// We receive the object that triggers the function as a parameter
exports.handler = async (event) => {
    // Extract values from event and format as strings
    let name = JSON.stringify(`Hello from Lambda, ${event.firstName} ${event.lastName}`);
    // Create a JSON object with our response and store it in a constant
    const response = {
        statusCode: 200,
        body: name
    };
    // Return the response constant
    return response;
};

我使用他們的數據設置了測試事件功能:

{
  "firstName": "Ada",
  "lastName": "Lovelace"
}

但是運行代碼產生了我的第一個錯誤: exports is not defined in ES module scope

對那個錯誤進行一些搜索讓我陷入了一個遠遠超出基本教程范圍的兔子洞,所以我將文件從 index.mjs 重命名為 index.js。 重建和噗,它工作得很好:

{
  "statusCode": 200,
  "body": "\"Hello from Lambda, Ada Lovelace\""
}

我不會完成創建數據庫的步驟,因為那不相關。 一切都很好,直到我用可以訪問數據庫的代碼替換了代碼——上面的消息出錯了。 為了簡化問題,我刪除了 Lambda 並使用上面的工作代碼和require語句重新構建它:

// Include the AWS SDK module
const AWS = require('aws-sdk');
// Define handler function, the entry point to our code for the Lambda service
// We receive the object that triggers the function as a parameter
exports.handler = async (event) => {
    // Extract values from event and format as strings
    let name = JSON.stringify(`Hello from Lambda, ${event.firstName} ${event.lastName}`);
    // Create a JSON object with our response and store it in a constant
    const response = {
        statusCode: 200,
        body: name
    };
    // Return the response constant
    return response;
};

運行生成此輸出:

{
  "errorType": "Runtime.ImportModuleError",
  "errorMessage": "Error: Cannot find module 'aws-sdk'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/index.mjs",
  "trace": [
    "Runtime.ImportModuleError: Error: Cannot find module 'aws-sdk'",
    "Require stack:",
    "- /var/task/index.js",
    "- /var/runtime/index.mjs",
    "    at _loadUserApp (file:///var/runtime/index.mjs:1000:17)",
    "    at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1035:21)",
    "    at async start (file:///var/runtime/index.mjs:1200:23)",
    "    at async file:///var/runtime/index.mjs:1206:1"
  ]
}

我在轉儲中看到的最奇怪的事情:它仍然引用index.mjs即使我刪除了文件(或重命名它 - 我嘗試了兩種方法)。 在在線 IDE 中,我只能訪問僅顯示 index.js 的文件樹。 我假設那是/var/task所以我不知道/var/runtime在哪里仍然包含對index.mjs的引用。

我期待着這個令人頭疼的問題的解決方案。 感覺它很簡單,應該可以正常工作!

該教程提供的代碼不適用於使用 NodeJS 18 的 Lambda 運行時。這有幾個原因:

  1. Node.js 18.x 使用AWS SDK v3 在您的代碼中,您正在嘗試導入 AWS SDK v2。 問題是,NodeJS 18.x 運行時沒有自動打包並准備使用的 AWS SDK 版本 2 這就是為什么您收到無法找到aws-sdk錯誤的原因。 較新的 v3 SDK 與舊的 v2 版本有很大不同。

  2. exports is not defined in ES module scope - 此錯誤的原因是 Node.js 18.x 運行時使用的是ECMAScript 模塊而不是 CommonJS 模塊 這就是您看到擴展名為.mjs的 JavaScript 文件的原因。 可以說,ECMAScript 模塊以不同的方式導出依賴項(使用export關鍵字),同樣它們使用import而不是require來導入依賴項。

由於您正在做教程,我建議您暫時堅持使用 NodeJS 16(或可用的更低版本)。 遷移到 NodeJS 18.x 並不像您預期的那樣是一項微不足道的任務,但只要您擁有必要的經驗,它就不是世界末日。 希望本教程將來會針對最新的可用運行時進行更新。

要使用 NodeJS 16.x 創建函數,您可以從列表中選擇運行時: 在此處輸入圖像描述

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM