Node.js
-
Install
Terminal window npm install beautiful-imageTerminal window pnpm add beautiful-imageTerminal window yarn add beautiful-image -
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`)
Result object
Section titled “Result object”| 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 |
How it works
Section titled “How it works”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.
Next steps
Section titled “Next steps”- See all available filter methods in the API Reference.
- Deploying to AWS Lambda? See the Lambda + S3 guide.