'How can I deploy a Serverless Framework application the macOS that uses the sharp library to AWS?

I'm developing a Serverless Framework application that is using the Node runtime and is deployed to AWS. One of my AWS Lambda functions uses the sharp library.

When I run the AWS Lambda function, the following error occurs:

'darwin-x64' binaries cannot be used on the 'linux-x64' platform. Please remove the 'node_modules/sharp/vendor' directory and run 'npm install'.

I believe this error is occurring because when I run the sls deploy command on my local computer, the application is packaged on macOS and then moved to AWS. I think the application needs to be packaged on an operating system using linux-x64.

How can I deploy my Serverless Framework from my computer and still be able to use the sharp library?



Solution 1:[1]

The description of https://www.npmjs.com/package/sharp suggests it is linux compatible.

I am unfamiliar with how to (or if you can) force nodes native package resolution to a separate OS. Try building your lambda zip inside a docker image https://hub.docker.com/_/alpine/

Solution 2:[2]

If you have not already, I would suggest following the Installing the AWS SAM CLI on macOS guide to insure you have the correct local environment for developing Serverless on macOS.

This process is designed for the macOS, and includes built-in support for Docker, so that you can build and deploy packages that are compatible with Lambda directly from your local computer.

Solution 3:[3]

For AWS lambda deployment with Sharp module, the following worked for me when using serverless, esbuild and serverless-esbuild. Changed the serverless.yml file with the below configuration. It is basically telling esbuild to download sharp again with the following --arch=x64 --platform=linux considering your lambda uses x64 arch. Check serverless-esbuild packager and packagerOptions options for more understanding.

esbuild:
    # keep existing configurations
    external:
      - sharp
    packagerOptions:
      scripts:
        - npm install --arch=x64 --platform=linux sharp

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Keynan
Solution 2
Solution 3