Error: Cannot find module 'node-fetch'\nRequire stack:\n- /var/task IN AWS Lambda

Error: Cannot find module 'node-fetch'\nRequire stack:\n- /var/task IN AWS Lambda

我在本地使用 node-fetch 时没有收到任何错误 package.json 也有 node-fetch 依赖项,但是当我在 aws lambda 中部署时,找不到此依赖项。 从 'node-fetch';

导入提取
{
  "errorType": "Runtime.ImportModuleError",
  "errorMessage": "Error: Cannot find module 'node-fetch'\nRequire stack:\n- /var/task/query1.js\n- /var/task/query1Main.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
  "trace": [
    "Runtime.ImportModuleError: Error: Cannot find module 'node-fetch'",
    "Require stack:",
    "- /var/task/query1.js",
    "- /var/task/query1Main.js",
    "- /var/runtime/UserFunction.js",
    "- /var/runtime/index.js",
    "    at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
    "    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
    "    at Object.<anonymous> (/var/runtime/index.js:43:30)",
    "    at Module._compile (internal/modules/cjs/loader.js:999:30)",
    "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)",
    "    at Module.load (internal/modules/cjs/loader.js:863:32)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:708:14)",
    "    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)",
    "    at internal/main/run_main_module.js:17:47"
  ]
}

您必须将外部包声明为 lambda 的层 https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html