What is datauri?
The datauri npm package is used to convert file paths or file buffers to Data URI scheme. This is particularly useful for embedding file data directly into HTML or CSS files.
What are datauri's main functionalities?
Convert File Path to Data URI
This feature allows you to convert a file path to a Data URI. The `format` method takes the file extension and the file path as arguments and returns an object containing the Data URI.
const Datauri = require('datauri');
const dUri = new Datauri();
const result = dUri.format('.png', 'path/to/file.png');
console.log(result.content);
Convert Buffer to Data URI
This feature allows you to convert a buffer to a Data URI. The `format` method takes the file extension and the buffer as arguments and returns an object containing the Data URI.
const Datauri = require('datauri');
const dUri = new Datauri();
const buffer = Buffer.from('some data');
const result = dUri.format('.txt', buffer);
console.log(result.content);
Convert File Path to Data URI (Async)
This feature allows you to convert a file path to a Data URI using an asynchronous method. The `promise` method takes the file path as an argument and returns a promise that resolves to the Data URI.
const Datauri = require('datauri');
const dUri = new Datauri();
const result = await dUri.promise('path/to/file.png');
console.log(result);
Other packages similar to datauri
base64-img
The base64-img package allows you to convert images to base64 strings and vice versa. While it is more focused on image files, it provides similar functionality for converting files to Data URIs.
file-base64
The file-base64 package is another alternative that focuses on converting files to base64 strings. It provides a simple API for encoding and decoding files, similar to datauri.
datauri
Module and Client to generate Data URI scheme.

MODULE
npm install --save datauri
const Datauri = require('datauri');
const datauri = new Datauri();
datauri.on('encoded', function (content) {
console.log(content);
});
datauri.on('error', function (content) {
console.log('Fail!');
});
datauri.encode('test/myfile.png');
Readable Stream
const Datauri = require('datauri');
const datauri = new Datauri();
datauri.pipe(process.stdout);
datauri.encode('test/myfile.png');
Promise (node 0.12+, works with es2016 async/await)
'use strict';
const DataURI = require('datauri').promise;
DataURI('test/myfile.png')
.then((content) => {
console.log(content);
}).catch((err) => {
throw err;
});
Callback for vintage users
const DataURI = require('datauri');
const datauri = new DataURI();
datauri.encode('test/myfile.png', function (err, content) {
if (err) {
throw err;
}
console.log(content);
console.log(this.mimetype);
console.log(this.base64);
console.log(this.getCSS());
console.log(this.getCSS({
class: "myClass",
width: true,
height: true
}));
});
Create from a string
const DataURI = require('datauri');
const datauri = new Datauri();
datauri.format('.png', 'xkcd');
console.log(datauri.content);
console.log(datauri.mimetype);
console.log(datauri.base64);
console.log(datauri.getCSS({
class: "myClass",
width: true,
height: true
}));
Create from a Buffer
If you already have your file as a Buffer, use this. It's much faster than passing a string.
var Datauri = require('datauri'),
dUri = new Datauri();
var buffer = fs.readFileSync('./hello');
dUri.format('.png', buffer);
console.log(dUri.content);
console.log(dUri.mimetype);
console.log(dUri.base64);
console.log(dUri.getCSS({
class: "myClass",
width: true,
height: true
}));
Chaining all stuff
datauri
.on('encoded', function (content) {
console.log(content);
console.log(this.mimetype);
console.log(this.base64);
console.log(this.getCSS());
console.log(this.getCSS({
class: "myClass"
});
})
.on('error', function (content) {
console.log('Fail!');
})
.encode('test/myfile.png');
Sync (kids! Don't use it at home!)
Sync Class
If DataURI class is instanciated with a file path, the same will be processed synchronously.
const Datauri = require('datauri');
let datauri = new Datauri('test/myfile.png');
console.log(datauri.content);
console.log(datauri.mimetype);
console.log(datauri.base64);
console.log(datauri.getCSS());
console.log(datauri.getCSS("myClass"));
Sync Function
const Datauri = require('datauri').sync;
console.log(Datauri('test/myfile.png'));
or for ES2015/6 lovers
import { sync as DataURI } from 'datauri';
console.log(DataURI('test/myfile.png'));
NPM SCRIPT AND TERMINAL CLIENT
GULP
GRUNT
There are a bunch of grunt plugins running on top of datauri module.
DEVELOPING
$ npm install
$ npm run check
To run test specs
$ npm run spec
If you'd like to test the full process including npm installer, just run:
$ npm run fulltest
Release notes
- 1.0 - async by default, native promise, streams, split between datauri and datauri-cli package
- 0.8 - remove node 0.8 support
- 0.7 - generate css background-image instead of background shorthand
- 0.6 - io.js support
- 0.5 - Format data uri from a string
- 0.4 - Promises support
- 0.3 - API Rewritten from the top to the bottom + full async compatibility
- 0.2 - Splitted in submodules mimer and templayed
- 0.1 - First release
License
MIT License
(c) Helder Santana