Sharp: Module initialization error on using Sharp in AWS lambda

Created on 26 Jul 2018  路  8Comments  路  Source: lovell/sharp

I create Lambda function (Node.js 6.10)

Basically, I'm trying to resize the S3 image and upload it again to another bucket. I chose Sharp for the resize method.

exports.handler = function(event, ctx, callback) {
...
function resizeImg(url, width, height) {
        request.get(url, function(err, res, body) {
            if (err) {
                reject(err);
            }
            sharp(body)
                .resize(width, height, { kernel: sharp.kernel.nearest })
                .toBuffer()
                .then(function(resizedBuffer) {
                    console.log('Resize image Successfully');
                }).catch(function(error) {
                    console.log(error);
                    return;
                });
        })
    }

...
}

I got error:

module initialization error: Error
at Error (native)
at Object.Module._extensions..node (module.js:597:18)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object. (/var/task/edg-image-resizer/node_modules/sharp/lib/constructor.js:10:15)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)

Here's my log of install the sharp:

$ npm i sharp
> [email protected] install /Users/xxxxx/Documents/Node/lambda-functions/abc-image-resizer/node_modules/sharp
> (node install/libvips && node install/dll-copy && prebuild-install) || (node-gyp rebuild && node install/dll-copy)

info sharp Downloading https://github.com/lovell/sharp-libvips/releases/download/v8.6.1/libvips-8.6.1-darwin-x64.tar.gz
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN [email protected] No description
npm WARN [email protected] No repository field.

+ [email protected]
added 75 packages from 93 contributors and audited 178 packages in 10.707s
found 0 vulnerabilities

Any suggestion is appreciated

question

Most helpful comment

remove sharp from node_modules and run: env npm_config_arch=x64 npm_config_platform=linux npm_config_target=8.10.0 npm install --save sharp

Change npm_config_target if you run node 6.10

All 8 comments

Where you building sharp? You know it must be in Amazon Linux Image, right?

Hi,
I'm getting exactly the same error and I used Linux for building. What could be the issue?

Hello, the mention of "darwin" in the line:

info sharp Downloading https://github.com/lovell/sharp-libvips/releases/download/v8.6.1/libvips-8.6.1-darwin-x64.tar.gz

suggests OS X is being used at npm install time but Linux is used at runtime.

Please see http://sharp.pixelplumbing.com/en/stable/install/#aws-lambda

remove sharp from node_modules and run: env npm_config_arch=x64 npm_config_platform=linux npm_config_target=8.10.0 npm install --save sharp

Change npm_config_target if you run node 6.10

Hi, thanks for all suggestions, I followed an instruction to deploy the function (nodejs) using Sharp to AWS Lambda:

  • Install and run Docker
  • in package, add : "dockerbuild": "rm -rf node_modules/sharp && docker run -v \"$PWD\":/var/task lambci/lambda:build-nodejs8.10 npm install" in script
  • Run npm run dockerbuild
  • Zip content and upload to Lambda

It worked for me. Share it here for ref

For windows using cmd, you can try this in package.json:

  "scripts": {
    "dockerbuild": "del /S /Q \"node_modules/sharp\" && docker run -v \"%cd%\":/var/task lambci/lambda:build-nodejs8.10 npm install"
  }

Then you can run npm run dockerbuild

@lovell I tried running the equivalent cmd above, when I deployed and tried to execute in Lambda (which has Node.js 8.10 runtime), I got this:

Something went wrong installing the "sharp" module
/var/task/node_modules/sharp/build/Release/../../vendor/lib/libvips-cpp.so.42: invalid ELF header

  • Ensure "linux" is used at install time as well as runtime

I believe the idea of using the pre-compiled binaries mentioned in #312 is to avoid having to use docker workarounds right? Do we have this for Windows? Or how can I solve the error I mentioned above?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

genifycom picture genifycom  路  3Comments

AVVS picture AVVS  路  3Comments

kachurovskiy picture kachurovskiy  路  3Comments

iq-dot picture iq-dot  路  3Comments

vermin1337 picture vermin1337  路  3Comments