Node.js Project to Convert SVG Image to Webp Format Using Sharp in Javascript Full Tutorial For Beginners

Node.js Project to Convert SVG Image to Webp Format Using Sharp in Javascript Full Tutorial For Beginners

 

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

 

 

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.

See also  jsPDF Tutorial to Generate PDF Invoice Template Using jsPDF-Autotable Library Full Example For Beginners

 

 

 

 

 

 

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

 

Leave a Reply