Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

Before bundle webpack, I did "npm i --arch=x64 --platform=linux --target=12.14.1 sharp" and labmda is working properly.

But, lambda upload zip size is more and more larger.

so, I want to resize lambda upload zip size using serverless-webpack.

Image lambda is working well except using sharp module.

I don't know how to do.

I did:

  1. Delete node_modules and package-lock.json and install dependencies ( also installed sharp )
  2. Delete node_modules/sharp and install sharp ( lambda environment - linux, x64, node version )
  3. Set serverless-webpack configuration in serverless : packagerOptions ( scrips ) - rebuild sharp lambda enviroment

But, lambda is not working properly.

and I looked many informations.

[lambda linux env]
Error running Sharp inside AWS Lambda function: darwin-x64' binaries cannot be used on the 'linux-x64' platform

[Serverless-webpack]
https://github.com/serverless-heaven/serverless-webpack/issues/396

Thank you!


[Edit]

My local env : Mac

production env : linux

Maybe, I think that npm command with "--platform" is not working in mac.

Additionally, I solved this problem using aws codebuild.

I posted answer.

But, It is not working in my local [ Mac ]

like image 460
hyundeock Avatar asked Oct 18 '25 09:10

hyundeock


1 Answers

I got it working by telling webpack to reinstall the package after it runs npm install:

webpack:
  includeModules:
    forceExclude:
      - aws-sdk
  packagerOptions:
    scripts:
      - rm -rf node_modules/sharp
      - npm install --arch=x64 --platform=linux sharp
like image 149
Dave Cowart Avatar answered Oct 20 '25 23:10

Dave Cowart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!