[英]Error: darwin-x64' binaries cannot be used on the 'linux-x64' platform (AWS lambda + typescript + webpack sharp module )
aws lambda with typescript is occured error when bundle by webpack. aws lambda 和 typescript 在由 webpack 捆绑时发生错误。
Before bundle webpack, I did "npm i --arch=x64 --platform=linux --target=12.14.1 sharp" and labmda is working properly.在捆绑 webpack 之前,我做了“npm i --arch=x64 --platform=linux --target=12.14.1 sharp”,labmda 工作正常。
But, lambda upload zip size is more and more larger.但是,lambda上传zip尺寸越来越大。
so, I want to resize lambda upload zip size using serverless-webpack.所以,我想使用 serverless-webpack 调整 lambda 上传 zip 的大小。
Image lambda is working well except using sharp module. Image lambda 除了使用夏普模块外工作良好。
I don't know how to do.我不知道该怎么做。
I did :我做了:
But, lambda is not working properly.但是,lambda 工作不正常。
and I looked many informations.我看了很多信息。
[lambda linux env] [λ linux 环境]
Error running Sharp inside AWS Lambda function: darwin-x64' binaries cannot be used on the 'linux-x64' platform 在 AWS Lambda function 中运行 Sharp 时出错:darwin-x64 二进制文件不能在“linux-x64”平台上使用
[Serverless-webpack] [无服务器-webpack]
https://github.com/serverless-heaven/serverless-webpack/issues/396 https://github.com/serverless-heaven/serverless-webpack/issues/396
Thank you!谢谢!
[Edit] [编辑]
My local env: Mac我的本地环境:Mac
production env: linux生产环境:linux
Maybe, I think that npm command with "--platform" is not working in mac.也许,我认为带有“--platform”的 npm 命令在 mac 中不起作用。
Additionally, I solved this problem using aws codebuild.此外,我使用 aws codebuild 解决了这个问题。
I posted answer.我发布了答案。
But, It is not working in my local [ Mac ]但是,它在我的本地 [Mac] 中不起作用
I got it working by telling webpack to reinstall the package after it runs npm install:我通过告诉 webpack 在运行 npm 安装后重新安装 package 来使其工作:
webpack:
includeModules:
forceExclude:
- aws-sdk
packagerOptions:
scripts:
- rm -rf node_modules/sharp
- npm install --arch=x64 --platform=linux sharp
I solved this problem using aws codebuild.我使用 aws codebuild 解决了这个问题。
codebuild has linux and node.js runtime. codebuild 有 linux 和 node.js 运行时。
So, I ran below command in aws codebuild ( https://sharp.pixelplumbing.com/install )因此,我在 aws codebuild 中运行了以下命令( https://sharp.pixelplumbing.com/install )
rm -rf node_modules/sharp
npm install --arch=x64 --platform=linux sharp
sharp module works properly.锋利的模块工作正常。
I got it working by using sam cli to build using this command我通过使用 sam cli 使用此命令构建它来工作
sam build -u
This command builds the code inside a container that has the similar environments as lambda此命令在与 lambda 具有相似环境的容器内构建代码
Dave Cowart's answer helped me a lot but having multiple lambda functions in the repo, I didn't want to install sharp into all of my functions. Dave Cowart 的回答对我帮助很大,但是在 repo 中有多个 lambda 函数,我不想在我的所有函数中安装Sharp。
Here's my solution:这是我的解决方案:
serverless.yml
webpack:
webpackConfig: 'webpack.config.js'
includeModules:
forceExclude:
- aws-sdk
packagerOptions:
scripts:
- rm -rf node_modules/sharp
- npm install --production --arch=x64 --platform=linux
You don't actually need to specify sharp in the install script.您实际上不需要在安装脚本中指定Sharp。 Making it a generic npm install means it will only reinstall sharp if it's in the package.json (which it won't be if it's not used due to webpack tree shaking).
Making it a generic npm install means it will only reinstall sharp if it's in the package.json (which it won't be if it's not used due to webpack tree shaking).
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.