在AWS Lambda函数中运行Sharp时出错:darwin-x64'二进制文件无法在'linux-x64'平台上使用 [英] Error running Sharp inside AWS Lambda function: darwin-x64' binaries cannot be used on the 'linux-x64' platform
问题描述
当尝试在AWS Lambda函数中快速运行时,我不断收到以下错误:
When attempting to run sharp inside an AWS Lambda function, I keep getting the following error:
darwin-x64'二进制文件不能在'linux-x64'平台上使用. 请删除"node_modules/sharp/vendor"目录并运行"npm" 安装"
darwin-x64' binaries cannot be used on the 'linux-x64' platform. Please remove the 'node_modules/sharp/vendor' directory and run 'npm install'
我使用MacBook Pro的无服务器框架部署无服务器应用程序.我该如何解决这个问题?
I deploy my serverless applications with Serverless Framework from my MacBook Pro. How do I fix this problem?
推荐答案
对 stdunbar 表示感谢,以帮助我正确地进行操作方向.
Kudos to stdunbar for steering me in the right direction.
以正常方式(例如:npm i sharp --save
)通过NPM在MacOS上安装Sharp时,安装程序会自动为OS X添加二进制文件.但是AWS lambda函数在具有x64处理器的Linux 2计算机上运行,这就是为什么出现此错误的原因
When installing sharp on MacOS via NPM the normal way (i.e.: npm i sharp --save
), the installer automatically adds binaries for OS X. But AWS lambda functions run on Linux 2 machines with x64 processors and this is why we get this error.
要修复此问题,必须先完全卸载Sharp,然后运行:
npm install --arch=x64 --platform=linux sharp
注意:
版本0.25不再与目标标志一起使用.这曾经可以工作:
version 0.25 no longer works with the target flag. This used to work:
npm install --arch=x64 --platform=linux --target=10.15.0 sharp
然后像往常一样使用sls deploy
旁注:
锋利无比的速度!!!在使用Sharp之前,我使用了另一个名为Jimp的图像大小调整实用程序.它完成了工作,但是速度很慢.为了防止出现超时错误,为了处理典型的1 MB图像,我不得不将内存大小从128增加到512,并将超时从5秒增加到30秒.
Sharp is EXTREMELY FAST!!! Before using sharp, I was using another image resizing utility named Jimp. It did the job, but was quite slow. To prevent timeout errors, I had to increase the memory size from 128 to 512 and the timeout from 5 seconds to 30 seconds just to handle a typical 1 megabyte image.
以下是两者之间的比较:使用相同的配置将1.2Mb图片缩小为600x400:
Here is a comparison between the two for resizing a 1.2Mb picture down to 600x400 using the same configuration:
Jimp->使用了512Mb内存,AWS向我收费14300 ms.
Jimp -> used 512Mb of memory and AWS billed me for 14300 ms.
Sharp->使用了132 MB的内存,AWS向我收费了800毫秒.
Sharp -> used 132 MB of memory and AWS billed me for 800 ms.
那比Jimp快14倍!
这篇关于在AWS Lambda函数中运行Sharp时出错:darwin-x64'二进制文件无法在'linux-x64'平台上使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!