Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

cwebp

Intervox8.3k3.2.0

node.js wrapper for cwebp and dwebp binaries from WebP image processing utility

webp, compress, minify, optimize, img, image

readme

WebP logo

NPM Package Build Status

node-webp

Node.js wrapper for cwebp and dwebp binaries from WebP image processing utility.

Installation

npm install cwebp

Getting latest version of WebP

You can get latest WebP source, pre-compiled binaries and installation instructions from its official website, or from its downloads repository.

Linux users may use this installation script to automatically download and install the latest WebP binaries:

curl -s https://raw.githubusercontent.com/Intervox/node-webp/latest/bin/install_webp | sudo bash

MacOS users may install WebP using homebrew:

brew install webp

or MacPorts:

sudo port selfupdate
sudo port install webp

If none of it suit your needs, you may build the WebP utilities yourself.

WebP versions compatibility

node-webp version Fully compatible WebP versions Partially compatible WebP versions
0.1.x all versions all versions
1.x 0.4.1 and later all versions
2.x 0.5.0 and later all versions

Usage

var CWebp = require('cwebp').CWebp;
var DWebp = require('cwebp').DWebp;

var encoder = new CWebp(source_image);
var decoder = new DWebp(source_webp);

or

// new is optional
var encoder = CWebp(source_image);
var decoder = DWebp(source_webp);

or

// Backward-compatibility with cwebp@0.1.x
var CWebp = require('cwebp');

Specifying path to cwebp binary

By default node-webp looks for cwebp and dwebp binary in your $PATH.

Specifying path as a constructor option

var Webp = require('cwebp');
var binPath = require('webp').cwebp;

var webp = new Webp(source, binPath);

Changing default behaviour

var CWebp = require('cwebp').CWebp;
CWebp.bin = require('webp').cwebp;

var encoder = new CWebp(source);
var DWebp = require('cwebp').DWebp;
DWebp.bin = require('webp').dwebp;

var decoder = new DWebp(source);

N.B.: webp npm module provide old webp 0.3.x binaries.

Available source types

When source is a string node-webp treats it as a file path.

var CWebp = require('cwebp').CWebp;
var DWebp = require('cwebp').DWebp;

var encoder = new CWebp('original.jpeg');
var decoder = new DWebp('converted.webp');

It also accepts Buffers and Streams.

var encoder = new CWebp(buffer);
var decoder = new DWebp(stream);

Note that node-webp will start consuming the input stream only when .write(), .stream() or .toBuffer() is called.

Encoding and decoding WebP images

encoder.write('image.webp', function(err) {
    console.log(err || 'encoded successfully');
});
decoder.write('image.png', function(err) {
    console.log(err || 'decoded successfully');
});

Getting output image as a Buffer

decoder.toBuffer(function(err, buffer) {
    // ...
});

Getting output image as a readable Stream

var stream = encoder.stream();

stream.pipe(destination);
stream.on('error', function(err) {
  // something bad happened
});

Using promises

node-webp supports promises.

encoder.write('image.webp').then(function() {
    // ...
});
encoder.toBuffer().then(function(buffer) {
    // ...
});
decoder.stream().then(function(stream) {
    // ...
});

Specifying conversion options

node-webp provides helper functions for most of cwebp and dwebp conversion options. For the full list of available helpers see methods.json file.

encoder.quality(60);
decoder.tiff();

Sending raw command

encoder.command('-d', 'dump.pgm');

Providing custom spawn options

encoder.spawnOptions({detached: true});

Changelog

changelog

3.2.0 / 2025-01-22

  • Updated installation scripts to install webp 1.5.0 #35

3.1.0 / 2024-04-18

  • Updated installation scripts to install webp 1.4.0 #32
  • Update Linux installation script to support aarch64 #32

3.0.0 / 2023-10-10

  • Use native Promise
  • Remove PassThrough polyfill
  • Drop support for Node.js version before 12.x

2.1.1 / 2023-10-10

  • Update Readme

2.1.0 / 2023-10-10

  • Add .spawnOptions() method #30
  • Updated installation scripts to install webp 1.3.2 #31
  • Run tests using GitHub Actions

2.0.5 / 2019-10-11

  • Updated installation scripts to install webp 1.1.0 #22

2.0.4 / 2019-10-11

  • Updated installation scripts to install webp 1.0.3 #20

2.0.3 / 2018-06-15

  • Updated installation scripts to install webp 1.0.0

2.0.2 / 2017-08-30

  • Updated installation scripts to install webp 0.6.0

2.0.1 / 2016-06-13

  • Add helpers for all new cwebp/dwebp switches

2.0.0 / 2016-06-09

  • Full stdio streaming support
  • BREAKING: WebP 0.4.x is no longer supported
  • BREAKING: NodeJS 0.8.x is no longer supported

1.1.0 / 2015-06-04

  • Updated raw-body to 2.x.x

1.0.7 / 2015-06-03

  • Fixed npm Readme

1.0.6 / 2015-06-03

  • Fixed error message on Windows

1.0.5 / 2015-05-04

  • Improved tmp directory lookup algorithm #7

1.0.4 / 2015-04-20

  • Fixed arguments exception #4

1.0.3 / 2014-09-24

  • Fixed webp installaton one-liner in readme

1.0.2 / 2014-09-24

  • Delayed stream ending\buffer promise fulfillment untill cleanup is complete #3

1.0.1 / 2014-09-22

  • Fixed deps problem

1.0.0 / 2014-09-22

  • Added native streaming support #2
  • Added support for input files, prefixed with - symbol #2
  • BREAKING: Changed .stream() behavior
  • BREAKING: Old versions of WebP (prior to 0.4.1) are no longer supported

0.2.0 / 2014-09-19

  • Added dwebp support #1
  • Updated installation script to install webp 0.4.1

0.1.10 / 2014-09-17

  • Fixed CoffeeScript compilation (removed top-level function wrapping)

0.1.9 / 2014-04-17

  • Updated deps, allowing wider range of when versions

0.1.8 / 2014-03-28

  • Fixed tmp files cleanup

0.1.7 / 2014-03-24

  • Updated dependencies to use when 3.0.x

0.1.6 / 2014-03-14

  • Added event listeners cleanup
  • Fixed tmp files leak after cwebp error processinf Buffer of Stream

0.1.5 / 2014-03-13

  • Fixed installation script to support old webp versions
  • Added verbose error reporting

0.1.4 / 2014-03-07

  • Added support for stringified numbers
  • Added bin option to specify cwebp path

0.1.3 / 2014-03-05

  • Improved cwebp errors reporting
  • Added install_webp script

0.1.2 / 2014-03-03

  • Fixed crush on non-function passed as a callback

0.1.1 / 2014-02-28

  • Fixed Readme

0.1.0 / 2014-02-28

  • Initial release