Converting PNG to WEBP with Node.js

This guide shows how to convert a PNG image to WEBP in Node.js with the Sharp package. If you would like to know more read the main article.

Install Sharp:

npm i sharp

Create a file (e.g. example.js) with the following contents:

example.js
import sharp from "sharp";

await sharp("image.png")
  .webp()
  .toFile("image.webp");

Run with:

node example.js