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.

Using Sharp on AWS Lambda Nodejs (8.10) - Unable to import module 'handler'

See original GitHub issue

Hey there!

I know there’s been a lot of AWS Lambda issues posted in the past, but having read them all and tried everything I can think of, I still can’t get Sharp to run in my Lambda.

Here’s the error I get:

Unable to import module 'handler': Error
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/var/task/libs/imagiser.js:4:14)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)

Now for some context, I have received this error in the past when trying to create some Lambda@Edge functions (using Nodejs 6.10), and the issue was the usual: I was uploading the Windows DLLs from my local Sharp installation, rather than compiling a Linux version and uploading that instead.

I fixed that issue and had Sharp working on my Lambda@Edge function, by setting up an EC2, compiling Sharp with Nodejs 6.10, downloading the node_modules folder and replacing my local files before deploying. This worked great and was done with the following script in my package.json:

rm -rf node_modules/sharp && copyfiles --up 1 _compiled/sharp/** node_modules

So now when I’ve attempted to use Sharp in my other Lambda functions (which use Nodejs 8.10), I figured all I’d need to do, is the same process, but compile Sharp using Nodejs 8.10.

Unfortunately this hasn’t worked, and I’ve tried all sorts of variations, versions etc. and none have worked.

At one point I got a different error:

module initialization error: Error
was compiled against a different Node.js version using
NODE_MODULE_VERSION 48. This version of Node.js requires
NODE_MODULE_VERSION 57. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
at Object.Module._extensions..node (module.js:681:18)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/var/task/node_modules/sharp/lib/constructor.js:10:15)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)

But I suspect this is just when I was using the Nodejs 6.10 compiled files. Every variation since has given me the initial error I posted above.

I guess my question is, is there any reason anyone can see why this wouldn’t be working? Or any specific steps needed to get Sharp working on Nodejs 8.10? Or maybe some other compatibility issue with this specific version? Or with Lambda itself?

I’m short on time to get this working, so any suggestions at all are greatly appreciated!

Thanks! Jay.

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
lovellcommented, Jun 11, 2018

Hello, are you sure the Unable to import module 'handler' message is coming from sharp? It suggests a Lambda (or Webpack, or serverless?) config problem.

https://github.com/search?q="Unable+to+import+module+'handler'"&type=Issues

1reaction
artboard-studiocommented, Oct 28, 2018

Running

rm -rf node_modules/sharp
docker run -v "$PWD":/var/task lambci/lambda:build-nodejs8.10 npm install

solved the problem

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolve the "Unable to import module" error for Lambda code ...
To resolve this error, create a deployment package or Lambda layer that includes the libraries that you want to use in your Node.js...
Read more >
Problem running AWS Lambda function with Sharp library
Try changing your local node version to 8.10 , remove node_modules , npm install , bundle folder including node_modules and upload to lambda....
Read more >
How do I resolve "Unable to import module" error ... - YouTube
AWS Knowledge Center Videos. How do I resolve " Unable to import module " error that I receive when I run Lambda code...
Read more >
How to use AWS Lambda Layers with SAM - BlowStack
Nodejs folder have to be created as a parent folder for layer dependencies (node_modules). RULE #2. Create layer resources and reference them in ......
Read more >
NodeJS Runtime Environment with AWS Lambda Layers
However, this can quickly get tiresome with growing number of Lambda functions. Repetitive installations of the same node modules for every ...
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