Skip to content

Instantly share code, notes, and snippets.

@DorkNstein
Created July 18, 2018 15:01
Show Gist options
  • Save DorkNstein/15baab8f928dca78dfa95a98a59e2cb1 to your computer and use it in GitHub Desktop.
Save DorkNstein/15baab8f928dca78dfa95a98a59e2cb1 to your computer and use it in GitHub Desktop.
Easy way to import local image files in nodejs.
const Module = require('module');
const fs = require('fs');
Module._extensions['.jpg'] = function(module, fn) {
let base64 = fs.readFileSync(fn).toString('base64');
module._compile('module.exports="data:image/jpg;base64,' + base64 + '"', fn);
};
Module._extensions['.png'] = function(module, fn) {
let base64 = fs.readFileSync(fn).toString('base64');
module._compile('module.exports="data:image/png;base64,' + base64 + '"', fn);
};
const image = require('./logo.jpg');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment