See code on Github

SVG64

Convert SVG to base64 anywhere

Installation

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');

demo.style.backgroundImage = `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);

console.log(result);

This is the SVG

This is the base64 encoded SVG used as a background image