See code on Github


Convert SVG to base64 anywhere


npm install svg64

# or

yarn add svg64

Usage in browser

import svg64 from 'svg64'; // or use window.svg64.default

const svg = document.getElementById('svg');
const b64 = svg64(svg);
const demo = document.getElementById('demo'); = `url(${b64})`;

Usage in NodeJS

const { resolve } = require('path');
const { readFileSync } = require('fs');

const svg64 = require('svg64');

const svg = readFileSync(resolve(__dirname, 'assets', 'example.svg'), 'utf-8');
const result = svg64(svg);


This is the SVG

This is the base64 encoded SVG used as a background image