Using Sharp on AWS Lambda Nodejs (8.10) - Unable to import module 'handler'
See original GitHub issueHey 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:
- Created 5 years ago
- Reactions:4
- Comments:8 (1 by maintainers)
Top GitHub Comments
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
Running
solved the problem