Aug 21, 2026
Converting SVG to PNG is usually the domain of packages such as Sharp. This creates a binary dependency which complicates deployment (develop on Mac, deploy on Linux). The resvg package gives you this functionality without the binary dependency thanks to the included WASM module.
const fs = require( 'fs' );
const path = require( 'path' );
const { Resvg, initWasm } = require( '@resvg/resvg-wasm' );
let wasmReady = false;
async function main( params ) {
const svg = `
<svg
width="640"
height="480"
viewBox="0 0 640 480"
xmlns="http://www.w3.org/2000/svg">
<style>
rect {
fill: ${params.backgroundColor ?? 'red'};
stroke: none;
}
</style>
<rect
x="270"
y="190"
width="100"
height="100"
rx="13"
ry="13" />
</svg>
`;
if( !wasmReady ) {
wasmReady = await initWasm( fs.readFileSync(
path.join( __dirname, 'index_bg.wasm' )
) );
}
await wasmReady;
const resvg = new Resvg( svg );
return {
statusCode: 200,
headers: {
'Content-Type': 'image/png'
},
body: Buffer.from( resvg.render().asPng() )
};
}
Back to Notes