Registry / storage / js-base64-file

js-base64-file

JSON →
library2.0.3jsnpmunverified

A Node.js library to load, convert, and save local and remote files (including images) to base64 strings or data URIs. Version 2.0.3 supports both async (callback/Promise) and sync methods. It handles all file types and uses node-fetch for remote file fetching. Key differentiator: simple API for base64 conversion with both local and remote file support.

npm install js-base64-file
INSTALL
IMPORT
SIG · JS-BASE64-FILE
J
js-base64-file
storagejavascriptv2.0.3
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.

Base64File
import { Base64File } from 'js-base64-file'
import Base64File from 'js-base64-file'
Named export only. Default import will result in undefined.
Base64File
const { Base64File } = require('js-base64-file')
const Base64File = require('js-base64-file')
CommonJS destructured require. The module is ESM, but also provides CJS via exports.
new Base64File()
const image = new Base64File()
const image = new base64File()
Constructor is case-sensitive. Must be PascalCase 'Base64File'.

Demonstrates sync, async, and remote file loading to base64 using the Base64File class.

import { Base64File } from 'js-base64-file'; const image = new Base64File(); const path = './'; const fileName = 'test.png'; // Sync load try { const data = image.loadSync(path, fileName); console.log('Base64 data:', data.slice(0, 50) + '...'); } catch (err) { console.error('Failed to load file:', err); } // Async load image.load(path, fileName, (err, data) => { if (err) { console.error(err); } else { console.log('Async base64:', data.slice(0, 50) + '...'); } }); // Remote load (requires node-fetch) const remoteURL = 'https://example.com/image.png'; const remoteFile = 'image.png'; image.loadRemote(remoteURL, remoteFile) .then(data => console.log('Remote base64:', data.slice(0, 50) + '...')) .catch(err => console.error(err));
Debug
Known issues
gotchaThe loadRemote method returns a Promise in v2 (not via callback). The README examples show using await, but older docs may show callback pattern.
fix
Use async/await or .then(). Do not pass a callback as third argument (third argument is for node-fetch options).
affects: >=2.0.0
gotchaThe path argument in load/loadSync must always end with a slash (e.g., './' not '.'). Otherwise it may cause incorrect file path concatenation.
fix
Ensure path ends with '/' (or '\\' on Windows).
affects: *
deprecatedIn v1, the export was 'Base64File' as default. In v2, it is a named export. Using default import will yield undefined.
fix
Use named import: import { Base64File } from 'js-base64-file'
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: Base64File is not a constructor
Using default import instead of named import in v2+
fix
Change 'import Base64File from ...' to 'import { Base64File } from ...'
Error: ENOENT: no such file or directory, open '...'
Missing trailing slash in path argument for load/loadSync
fix
Ensure path ends with '/' (e.g., './' instead of '.')
ReferenceError: fetch is not defined
loadRemote requires node-fetch dependency in Node.js environment
fix
Install node-fetch: npm install node-fetch
Upgrade
Version history
2.0.3latest on npm
Audit
Dependencies
node-fetchrequiredRequired for loadRemote method to fetch remote files
fsrequiredCore Node.js module for file system operations
Agent activity
17 hits · last 30 days
node
16
OpenAI (training)
1
Resources
js-base64-file — npm install js-base64-file · libregistry