sharp: sharp is not working ( install ) in aws lambda ( serverless-webpack )

[I used]
1. serverless framework ( aws lambda, aws-nodejs-typescript template )
2. node version 12.14.1
3. sharp version 0.25.2 ( I did test 0.25.0 ~ 0.25.3 )

I want to resize my lambda zip size.

So I did install serverless-webpack plugins and bundle my lambda.

Before bundled by webpack, only transpile typescript to javascript and deploy using serverless and works well.

But, lambda bundled by serverless-webpack is not working.

[Error message] darwin-x64’ binaries cannot be used on the ‘linux-x64’ platform. Please remove the ‘node_modules/sharp/vendor’

I don’t know how to do.

I did check node_modules/sharp/vendor and saw linux-64 target.

but lambda is darwin-x64 logged.

I don’t want to use lambda layer.

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

upload and delete lambda work well and resized zip size!!

but get ( resize image ) lambda is not.

Please help me.

Thank you

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 4
  • Comments: 19 (4 by maintainers)

Most upvoted comments

I fixed this issue by forcing the webpack to run the scripts I am using serverless-bundle So the solution to fix it, it was the following …

[serverless.yml]

plugins:
  - serverless-offline
  - serverless-pseudo-parameters
  - serverless-iam-roles-per-function
  - serverless-bundle

custom:
  bundle:
    packagerOptions:
      scripts:
        - rm -rf node_modules/sharp
        - npm install --arch=x64 --platform=linux sharp

https://sharp.pixelplumbing.com/install#aws-lambda https://www.npmjs.com/package/serverless-bundle

Thank you…

I wrote that post…

For those using esbuild plugin

custom: {
    esbuild: {
      bundle: true,
      minify: false,
      sourcemap: true,
      target: 'node14',
      define: { 'require.resolve': undefined },
      platform: 'node',
      concurrency: 10,
      external: ['sharp'],
      packagerOptions: {
        scripts: [
          'rm -rf node_modules/sharp',
          'npm install --arch=x64 --platform=linux sharp',
        ],
      },
    },
  },

I did change module ( sharp to jimp )

Jimp is working. ( zero native dependency )

But, Jimp slower than sharp…

Jimp : https://www.npmjs.com/package/jimp

sharp and jimp performance : https://sharp.pixelplumbing.com/performance