1

When I invoke any function in my serverless application I get an error as below. However, directly installing sharp give me no errors. also, when I run the tests sharp works perfectly.

Missing module": /build/Release/sharp-darwin-x64.node" exists in the node modules

Error:
Something went wrong installing the "sharp" module

Cannot find module '../build/Release/sharp-darwin-x64.node'
Require stack:
- /.esbuild/.build/src/functions/function1/handler.js

System:

  • serverless offline
  • node12 (x64)
  • m1 chip (arm64)
  • Serverless esbuild plugin used

solutions tried:

mamadou jallow
  • 351
  • 1
  • 2
  • 10
  • The way i solved it was to just use the serverless bundle which has a section for sharp in its documentation: https://www.npmjs.com/package/serverless-bundle – mamadou jallow Jan 06 '22 at 15:09

2 Answers2

1

According to the official documentation, try installing sharp.

npm install --arch=x64 --platform=darwin sharp

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
0

I was not able to get the esbuild solution working for local debugging. Instead, I modified my deployment scripts to always re-install the correct version of sharp before deploying.

The idea being I install the correct version of sharp before each scenario.

Here are my scripts in package.json

  "scripts": {
    "debug": "npm run clean && npm install sharp && serverless invoke local --function annotate --path ./sampleEvent.json",
    "clean": "rm -rf node_modules/sharp",
    "sharp": "SHARP_IGNORE_GLOBAL_LIBVIPS=1 npm install --arch=x64 --platform=linux --libc=glibc sharp",
    "deploy-prod": "npm run clean && npm run sharp && sls deploy --stage production",
    "deploy-dev": "npm run clean && npm run sharp && sls deploy --stage development"
  },
epietrowicz
  • 334
  • 1
  • 4
  • 16