Registry / http-networking / js-file-download

js-file-download

JSON →
library0.4.12jsnpmunverified

The `js-file-download` package offers a focused JavaScript utility function to initiate browser-driven file downloads directly from client-side data. It enables applications to save JavaScript-generated content, such as dynamically created CSVs, JSON blobs, or any string data, into a local file on the user's system without requiring a server roundtrip. The current stable version, 0.4.12, includes recent fixes for specific browser compatibility issues, notably on Apple devices, indicating ongoing maintenance. Its release cadence appears to be driven by bug fixes and compatibility updates rather than feature additions. Key differentiators include its lightweight footprint, single-function API, and direct utility for client-side data persistence, making it an efficient choice for scenarios where data is already available in the browser and needs to be downloaded.

npm install js-file-download
INSTALL
IMPORT
SIG · JS-FILE-DOWNLOAD
J
js-file-download
http-networkingjavascriptv0.4.12
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

fileDownload
import fileDownload from 'js-file-download';
import { fileDownload } from 'js-file-download';
The package exports a default function. Named import will result in 'TypeError: fileDownload is not a function'.
fileDownload (CJS)
const fileDownload = require('js-file-download');
const { fileDownload } = require('js-file-download');
The README shows CommonJS `require` syntax, which correctly accesses the default export. Destructuring will fail.
Type import
import fileDownload from 'js-file-download'; // Includes types
import type { fileDownload } from 'js-file-download';
The package ships TypeScript types. The default import statement handles both the function and its type. Attempting to import types with a named import for a default export is incorrect.

Demonstrates how to download various types of client-side generated content (text, JSON, CSV) using the `fileDownload` function, including specifying MIME types.

import fileDownload from 'js-file-download'; // Example 1: Download a simple text string const textData = 'Hello, world!\nThis is some client-generated text.'; fileDownload(textData, 'hello.txt'); // Example 2: Download JSON data const jsonData = { id: 1, name: 'Product A', description: 'A fantastic product generated on the client-side.', timestamp: new Date().toISOString(), }; const jsonString = JSON.stringify(jsonData, null, 2); fileDownload(jsonString, 'data.json', 'application/json'); // Example 3: Simulate downloading a CSV file with custom content type const csvData = `Name,Age,City\nAlice,30,New York\nBob,24,London\nCharlie,35,Paris`; fileDownload(csvData, 'report.csv', 'text/csv'); // In a browser environment, these calls would trigger actual downloads. // For security and browser sandbox reasons, actual file saving does not happen in a Node.js console. console.log('File download attempts initiated. Check your browser downloads.'); // Illustrative environment variable usage, not directly related to file-download functionality. const API_KEY = process.env.MY_API_KEY ?? 'sk-example-key'; if (API_KEY === 'sk-example-key') { console.warn('Warning: MY_API_KEY environment variable is not set. Using a dummy key.'); }
Debug
Known issues
gotchaThe package exports a default function. Attempting to use named imports (`import { fileDownload } from 'js-file-download';`) will result in a runtime `TypeError`.
fix
Always use a default import: `import fileDownload from 'js-file-download';` for ESM, or `const fileDownload = require('js-file-download');` for CommonJS.
affects: >=0.1.0
gotchaWhile the package aims for broad browser compatibility, historical issues, such as those fixed in v0.4.12 for Apple devices, highlight potential cross-browser quirks. Always test thoroughly in target browsers.
fix
Ensure you are on the latest stable version of `js-file-download` to benefit from recent browser compatibility fixes.
affects: <0.4.12
gotchaDownloading very large files generated client-side can lead to browser memory exhaustion or performance issues, especially on less powerful devices. The content is held in memory before download.
fix
For extremely large datasets, consider server-side file generation or streaming approaches if feasible, rather than purely client-side generation and download.
affects: >=0.1.0
gotchaIncorrectly specifying the MIME type (the third argument) or omitting it for non-textual content can lead to browsers handling the downloaded file incorrectly (e.g., displaying binary data as text).
fix
Always provide the correct MIME type as the third argument (e.g., 'application/json', 'text/csv', 'image/png') to ensure proper browser handling.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: fileDownload is not a function
Attempting to import `fileDownload` as a named export when it is a default export.
fix
Change your import statement from `import { fileDownload } from 'js-file-download';` to `import fileDownload from 'js-file-download';`.
ReferenceError: fileDownload is not defined
This usually happens when mixing CommonJS (`require`) and ES Module (`import`) syntax without proper transpilation, or if the module isn't correctly resolved in the environment.
fix
Ensure your environment (Node.js, bundler like Webpack/Rollup) is configured to handle both module types, or stick to the appropriate syntax for your module system (e.g., `const fileDownload = require('js-file-download');` in CJS files).
Downloaded file content appears garbled or with incorrect characters (e.g., mojibake for non-ASCII text)
Encoding issues, often due to the browser defaulting to an incorrect character set or missing an explicit UTF-8 byte order mark (BOM).
fix
Ensure your content is UTF-8 encoded. For text files, you might need to prepend a UTF-8 BOM if issues persist, or explicitly set the charset in the MIME type, e.g., `'text/csv;charset=utf-8;'`.
Upgrade
Version history
0.4.12latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
js-file-download — npm install js-file-download · libregistry