如何解决Aws-lambda函数中的nodejs版本问题

时间:2019-07-10 06:04:36

标签: node.js amazon-web-services aws-lambda node-modules sharp

我正在尝试通过Aws lambda调整图像大小并出现错误 我正在关注这篇文章

https://aws.amazon.com/blogs/compute/resize-images-on-the-fly-with-amazon-s3-aws-lambda-and-amazon-api-gateway/

{
  "errorType": "Error",
  "errorMessage": "The module '/var/task/node_modules/sharp/build/Release/sharp.node'\nwas compiled against a different Node.js version using\nNODE_MODULE_VERSION 48. This version of Node.js requires\nNODE_MODULE_VERSION 64. Please try re-compiling or re-installing\nthe module (for instance, using `npm rebuild` or `npm install`).",
  "trace": [
    "Error: The module '/var/task/node_modules/sharp/build/Release/sharp.node'",
    "was compiled against a different Node.js version using",
    "NODE_MODULE_VERSION 48. This version of Node.js requires",
    "NODE_MODULE_VERSION 64. Please try re-compiling or re-installing",
    "the module (for instance, using `npm rebuild` or `npm install`).",
    "    at Object.Module._extensions..node (internal/modules/cjs/loader.js:730:18)",
    "    at Module.load (internal/modules/cjs/loader.js:600:32)",
    "    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:531:3)",
    "    at Module.require (internal/modules/cjs/loader.js:637:17)",
    "    at require (internal/modules/cjs/helpers.js:22:18)",
    "    at Object.<anonymous> (/var/task/node_modules/sharp/lib/constructor.js:8:15)",
    "    at Module._compile (internal/modules/cjs/loader.js:701:30)",
    "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)",
    "    at Module.load (internal/modules/cjs/loader.js:600:32)"
  ]
}

1 个答案:

答案 0 :(得分:0)

修复它的步骤:-

  1. 使用Action-> Export函数从AWS Lambda下载项目(函数)并解压缩。

enter image description here

  1. 在本地系统中安装最新版本的节点(例如节点8.10.0)。您可以看到我已经有8.10.0。

enter image description here

  1. 通过终端转到解压缩的项目文件夹,然后键入

      

    npm重建

enter image description here

  1. 这之后再次压缩该项目并在AWS Lambda上对其进行升级,然后选择更新时使用的节点运行时(假设它是节点8.10.0,然后在aws lambda函数运行时中选择运行时作为节点8.10x)。

希望有帮助。

谢谢