js-file-download

A tiny JavaScript function that triggers the browser to save in-memory data as a downloadable file.

Library
npm
v0.4.12
920stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
43/100Fair
Development Activity0
Maintenance20
Community64
Maturity60
Momentum28

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
39/100Needs Attention
Architecture50
Code Quality25
Innovation25
Learning Curve55

js-file-download is a small, dependency-free utility that lets client-side JavaScript trigger a native browser “Save As” download for in-memory data — CSV exports, generated text, or blobs returned from an API — without a server-side redirect or a pre-hosted file URL.

It works by wrapping the given data in a Blob, creating an object URL, and programmatically clicking a hidden anchor tag with the HTML5 download attribute, falling back to msSaveBlob for older Internet Explorer. The whole implementation is a single file under 40 lines, ships its own TypeScript typings, and has zero runtime dependencies, making it a common building block in React and other front-end apps that need to let users download server responses or client-generated content as files.

What You Get

  • A single fileDownload(data, filename, mime, bom) function with zero runtime dependencies
  • Automatic Blob wrapping so you can pass raw strings, ArrayBuffers, or typed arrays directly
  • Legacy Internet Explorer support via the msSaveBlob fallback
  • Bundled TypeScript type declarations (js-file-download.d.ts)

Common Use Cases

  • Downloading a CSV or spreadsheet export generated client-side from a data table
  • Saving a binary file (PDF, image, zip) returned by an API call made with Axios or fetch
  • Letting users export JSON or configuration data as a downloadable file
  • Adding a byte-order-mark (BOM) to force correct encoding in downloaded text files opened in Excel

Under The Hood

Architecture The entire library is a single CommonJS module (file-download.js) exporting one function. There are no internal layers or abstractions to trace: the function branches once on window.navigator.msSaveBlob to pick between the legacy Internet Explorer save path and the modern object-URL-plus-hidden-anchor path used by every other browser, then cleans up the anchor element and revokes the object URL inside a setTimeout to avoid a known WebKit blob-resource error. Because the surface area is a single call with no state and no configuration object, there is nothing that would meaningfully “break” from a refactor beyond the function body itself.

Tech Stack The library targets the browser DOM directly — Blob, window.URL.createObjectURL/webkitURL, and document.createElement('a') — with no framework or runtime dependency. It ships as a CommonJS module via module.exports, is distributed on npm as plain, untranspiled ES5-compatible JavaScript (a .babelrc exists but only backs the ESLint parser, not a build step), and includes a hand-written js-file-download.d.ts declaration file referenced via the typings field in package.json for TypeScript consumers.

Code Quality There are no automated test files in the repository; the package.json test script simply runs npm run lint (ESLint with the eslint:recommended ruleset), so correctness has historically relied on manual verification and community bug reports rather than an assertion-based test suite. Error handling is minimal by design — the function has no failure modes to guard beyond browser API availability, which it already branches on. Naming is plain and single-purpose, and the codebase carries no linting or CI beyond a Travis config pinned to Node 8.

What Makes It Unique The technique — wrap data in a Blob, create an object URL, click a hidden anchor with a download attribute — is a well-known, widely reused browser pattern (also seen in libraries like FileSaver.js) rather than something original to this project. Its value is in packaging that pattern, including the older IE msSaveBlob fallback and the WebKit blob-revocation timing fix, into a single dependency-free function so consumers don’t have to hand-roll and re-debug the cross-browser quirks themselves.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search