Skip to content

Node.js

  1. Install

    Terminal window
    npm install beautiful-image
  2. Import from the Node.js entry point

    import { image } from 'beautiful-image/node'
    import { readFileSync, writeFileSync } from 'node:fs'
    const input = readFileSync('./photo.jpg')
    const result = await image(input)
    .resize(1200)
    .sharpen()
    .toJpeg(80)
    writeFileSync('./optimized.jpg', result.data)
    console.log(`${result.originalSize}B → ${result.optimizedSize}B`)
Field Type Description
data Buffer Optimized image as a Node.js Buffer
originalSize number Original file size in bytes
optimizedSize number New file size in bytes
compressionRatio number 0.85 means 85% smaller than original

Unlike the browser build, the Node.js pipeline runs the full decode → resize → filter → encode flow inside WASM. This means no dependency on sharp, libvips, or any native module, making it ideal for cold-start sensitive environments like Lambda.