Reputation: 1
I created a lambda function on aws using imangemin and imagemin-optipng nodejs plugin but it is returning me below error:
error: spawn /var/task/node_modules/optipng-bin/vendor/optipng ENOENT
var aws = require('aws-sdk');
var s3 = new aws.S3()
var Imagemin = require('imagemin');
var optipng = require('imagemin-optipng');
exports.handler = function(event, context, callback){
var srcBucket = event.Records[0].s3.bucket.name;
var srcKey =
decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " "));
var params = {Bucket: srcBucket, Key: srcKey};
s3.getObject(params).promise()
.then(data => Imagemin.buffer(data.Body, {
plugins: [
optipng({ optimizationLevel: 7 })
]
})
)
.then(buffer =>
console.log('done ', buffer)
)
.catch(err => callback(err))
};
Upvotes: 0
Views: 998
Reputation: 139
I just had a similar issue yesterday, on AWS Lambda. In case someone is also facing it, and the development environment is Windows, then I believe this is the solution for you. (note that here in my example I'm using Serverless Framework for building and deploying, however, the principle should work regardless of the use of Serverless)
I tried a few different solutions, but the easiest and fastest solution was to install the Windows Subsystem for Linux and run Serverless Deploy from the Ubuntu terminal on windows.
The issue is that some packages are OS-dependent, meaning that the same package installed on different OSs are going to produce different installations. Therefore your locally build/run works fine because you installed the packages on Windows environment and you are running the packages code on Windows environment, however, when you deploy to AWS it is now running on Amazon Linux and your packages that are OS-dependent (like mozjpeg, jpegtran, etc) are going to fail during the run. So your best shot is to just install the packages, build and deploy your project from a Linux environment (not sure if all Linux distros fit in this statement, but Ubuntu certainly does).
Here's the timeline for what I did:
- Install and enable the WSL with Ubuntu (no big deal at all, 10min top, just follow Microsofts doc)
- Open the Ubuntu terminal as an administrator (if you don't run as administrator it won't allow you to properly run "npm install" in the next steps)
- Make sure everything is updated, just run "apt install upgrade"
- Create a folder by running "mkdir your-folder-name" (or just cd directly into your project's original folder, you can do it by Shift+RightClick on the given folder and choosing "Open Linux Shell Here". I preferred to separate it to avoid messing with my original stuff)
- Get into the newly created folder by running "cd your-folder-name"
- Clone your repository into that folder or just copy/paste it manually (to open your current folder from Ubuntu terminal on Windows just run "explorer.exe .")
- Run the good and old "npm install" from the Ubuntu terminal
- Now here's a pitfall, if you have your AWS KEYS/SECRETS on your .env file, and you set your serverless.yml file to use the environment variables from the .env file, the next step will fail if you don't have the .env file in place (and you will only see the real error on CloudWatch because the console logged error in the browser will be a CORS error)
- Run "Serverless Deploy" to deploy your project
That's it. Took me around 20min to perform this solution while the others, even though some being effective (like CodeBuild), were more confusing therefore more time-consuming.
Upvotes: 1
Reputation: 55
try Reinstall optipng-bin module. or node ./node_modules/optipng-bin/lib/install.js
Upvotes: 0