A modern file-saver library with ESM support for downloading files in the browser
npm install @larrym/file-saverA modern, lightweight file-saver library with full ESM support for downloading files in the browser.
- âĻ Full ESM (ES Modules) support
- ðĶ CommonJS compatible
- ð· TypeScript support with full type definitions
- ð Browser-based file downloads
- ðŊ Multiple file format support (Text, JSON, CSV, Blob)
- ðĨ URL-based file downloads
- ð Zero dependencies
- ðŠķ Lightweight
``bash`
npm install @larrym/file-saver
`typescript
import { saveAs, saveText, saveJSON, saveCSV, downloadURL } from '@larrym/file-saver';
// Save a blob
const blob = new Blob(['Hello, World!'], { type: 'text/plain' });
saveAs(blob, 'hello.txt');
// Save text
saveText('Hello, World!', 'hello.txt');
// Save JSON
const data = { name: 'John', age: 30 };
saveJSON(data, 'data.json', true); // true = pretty print
// Save CSV
const csvData = [
['Name', 'Age'],
['John', '30'],
['Jane', '25']
];
saveCSV(csvData, 'data.csv');
// Download from URL
await downloadURL('https://example.com/file.pdf', 'document.pdf');
`
`javascript
const { saveAs, saveText, saveJSON } = require('@larrym/file-saver');
saveText('Hello, World!', 'hello.txt');
`
`typescript
import fileSaver from '@larrym/file-saver';
fileSaver.saveText('Hello, World!', 'hello.txt');
`
Save any data as a file.
- data: Blob | string | ArrayBuffer - The data to savestring
- fileName: - The name of the fileSaveOptions
- options: (optional)autoBom
- : boolean - Automatically add BOM for text files (default: true)
`typescript`
const blob = new Blob(['data'], { type: 'text/plain' });
saveAs(blob, 'file.txt');
Save text content as a file.
- text: string - The text contentstring
- fileName: - The name of the file
`typescript`
saveText('Hello, World!', 'hello.txt');
Save JSON data as a file.
- data: unknown - The data to save as JSONstring
- fileName: - The name of the fileboolean
- pretty: - Format with indentation (default: true)
`typescript`
const data = { message: 'Hello' };
saveJSON(data, 'data.json', true);
Save CSV data as a file.
- csvData: string | string[][] - CSV string or array of arraysstring
- fileName: - The name of the file
`typescript`
const data = [
['Name', 'Email'],
['John', 'john@example.com']
];
saveCSV(data, 'contacts.csv');
Download a file from a URL.
- url: string - The URL to download fromstring
- fileName: - The name to save the file asPromise
- Returns:
`typescript`
await downloadURL('https://example.com/image.jpg', 'photo.jpg');
This library works in all modern browsers that support:
- Blob API
- URL.createObjectURL
- HTML5 download attribute
Full TypeScript support is included with type definitions.
`typescript
import { saveAs, SaveOptions } from '@larrym/file-saver';
const options: SaveOptions = {
autoBom: true
};
const blob = new Blob(['data']);
saveAs(blob, 'file.txt', options);
`
This package uses npm's trusted publishing feature with OIDC authentication for secure, token-free publishing from GitHub Actions.
1. Create or login to your npm account at https://www.npmjs.com
2. Navigate to package settings:
- Go to your package page (or create a new scoped package placeholder)
- Click on "Settings" tab
- Scroll to "Publishing access" section
3. Add trusted publisher:
- Click "Add trusted publisher"
- Select "GitHub Actions"
- Fill in the details:
- Organization/Username: larrymotalavignefile-saver
- Repository: publish.yml
- Workflow filename: npm-publish
- Environment name:
4. Save the configuration
1. Create a new release on GitHub:
`bash`
git tag v1.0.0
git push origin v1.0.0
2. Go to GitHub â Releases â Create a new release
3. The GitHub Action will automatically publish to npm using OIDC authentication
You can also trigger the workflow manually from the Actions tab in GitHub.
- Node.js >= 18.0.0
- npm >= 11.5.1 (for OIDC trusted publishing)
`bashInstall dependencies
npm install
MIT ÂĐ Larry Motalavigne
Contributions are welcome! Please feel free to submit a Pull Request.
This package uses npm's trusted publishing with OIDC, which provides:
- No long-lived tokens to manage or leak
- Cryptographic verification of publisher identity
- Automatic provenance attestations
- Enhanced supply chain security
If you encounter any issues or have questions, please file an issue on the GitHub repository.