Registry / storage / fetch-base64

fetch-base64

JSON →
library2.1.2jsnpmunverified

A Node.js package (v2.1.2, last updated 2019) for fetching local or remote files and returning them as base64-encoded strings with optional MIME type prefixes. Useful for inlining assets like images or fonts into HTML/CSS. Provides three functions: `local`, `remote`, and `auto` for path detection. No dependencies, simple API, but no longer actively maintained. Alternative to data URIs with manual encoding.

npm install fetch-base64
INSTALL
IMPORT
SIG · FETCH-BASE64
F
fetch-base64
storagejavascriptv2.1.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
const fetch = require('fetch-base64');
import fetch from 'fetch-base64';
Package does not export ESM; require() is the only supported import style.
local
const { local } = require('fetch-base64');
Destructure the required object to access individual functions.
remote
const { remote } = require('fetch-base64');
Similarly, destructure for remote function.
auto
const { auto } = require('fetch-base64');
Destructure for auto function.

Shows how to require the package and use local, remote, and auto functions to fetch files and get base64 encoded data with MIME type.

const fetch = require('fetch-base64'); // Fetch a local image fetch.local('/path/to/image.jpg') .then(data => { console.log('Raw base64:', data[0]); console.log('With MIME:', data[1]); }) .catch(err => console.error(err)); // Fetch a remote image fetch.remote('http://example.com/image.png') .then(data => { console.log('With MIME:', data[1]); }) .catch(err => console.error(err)); // Auto-detect local vs remote fetch.auto('./local/file.gif', 'http://example.com/file.gif') .then(data => console.log(data[1])) .catch(err => console.error(err));
Debug
Known issues
gotchaThe return value is an array with two strings: data[0] (raw base64) and data[1] (with MIME prefix). Do not confuse them; data[1] is typically used for embedding.
fix
Always use data[1] if you need a data URI.
affects: >=1.0.0
gotcharemote() spoofs the User-Agent to Chrome's but may still fail if the server blocks requests or requires authentication.
fix
Pass custom headers via the options object: fetch.remote({ url, headers: { Authorization: '...' } })
affects: >=1.0.0
gotchaauto() determines remote/local based on the first argument only. If you pass multiple arguments, it treats all as local if the first is a file path.
fix
Use explicit local() or remote() calls instead of auto() when mixing types.
affects: >=1.0.0
deprecatedPackage not updated since 2019; may have compatibility issues with newer Node versions (though it uses basic http/https).
fix
Consider alternatives like 'request' (deprecated) or using native fetch with Buffer encoding.
affects: >=2.0.0
Errors
Common errors & fixes
fetch.remote is not a function
Incorrect import: using ES6 import syntax with a package that only supports CommonJS.
fix
Use require('fetch-base64') instead of import.
Cannot read property '0' of undefined
Promise rejected but .catch not handling error, or data is undefined.
fix
Add a .catch handler to log the error: promise.catch(err => console.error(err)).
TypeError: path must be a string
Passing non-string arguments to local() or auto().
fix
Ensure all path arguments are strings.
Upgrade
Version history
2.1.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
31 hits · last 30 days
node
28
Amazon
1
OpenAI (training)
1
Resources
fetch-base64 — npm install fetch-base64 · libregistry