Welcome folks today in this blog post we will be converting svg
to webp
format using sharp
library. All the source code of the project will be given below.
Get Started
In order to get started we need to install the sharp
npm package by installing by this command
npm i sharp
And after installing this we need to make a index.js
file and copy paste the following code
index.js
1 2 3 4 5 6 7 8 9 10 11 |
const sharp = require("sharp") sharp("image.svg") .webp() .toFile("new-file.webp") .then(function(info) { console.log(info) }) .catch(function(err) { console.log(err) }) |
Now you can see in the above code we are importing the sharp
library at the very beginning and then we are using this function to convert the svg
image to webp
format. It contains the callback function in the form of promises
. When the conversion is completed we use the then
callback and then we get the info
object and if any error takes place we have the catch
callback to catch any error if any error takes place.
As you can see we have the file my-file.webp
created after you run the node.js
script by running the command
node index.js