question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Module initialization error on using Sharp in AWS lambda

See original GitHub issue

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.<anonymous> (/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
> sharp@0.20.5 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 edg-image-resizer@1.0.0 No description
npm WARN edg-image-resizer@1.0.0 No repository field.

+ sharp@0.20.5
added 75 packages from 93 contributors and audited 178 packages in 10.707s
found 0 vulnerabilities

Any suggestion is appreciated

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
tobiastornroscommented, Aug 7, 2018

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

4reactions
francophongvucommented, Aug 13, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolve the "Unable to import module" error for Lambda code ...
I receive the "Unable to import module" error when I try to run my AWS Lambda code in Node.js. Short description. You typically...
Read more >
Problem running AWS Lambda function with Sharp library
I believe that the sharp library is the problem, so I have tried to change the version to the latest to see if...
Read more >
Cannot find module (AWS Lambda Error) [Solved] | bobbyhadz
The "Cannot find module" occurs when a lambda function is trying to access a module which is not available in the function's execution...
Read more >
[Solved]-AWS Lambda : module initialization error-node.js
downgrade the version of Node.js to 6.10.0 . Use, for example nvm · remove your node_modules/ · install all modules with npm i...
Read more >
Module initialization error lambda
camaro is a native module. you will need to install correct prebuilt binary for AWS Lambda.. Even though you switch to Node 6.10...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found