download-file
download-file is a minimalist Node.js utility for asynchronously downloading files from a given URL to a specified local directory. Currently at version 0.1.5, the package operates exclusively using a traditional Node.js callback-based API, lacking modern Promise or async/await support. Its release cadence appears to be very slow or halted, suggesting it is either in a long-term maintenance mode or effectively abandoned. The package provides a simple interface, requiring just a URL and an optional configuration object for the target directory and filename. It focuses solely on basic file transfer and local storage, without advanced features such as progress tracking, automatic retries, or robust error handling for network interruptions, file system issues beyond basic `ENOENT`, or `AbortController` integration. This simplicity positions it as a lightweight solution for very basic, fire-and-forget download tasks within a Node.js environment.
Common errors
-
Error: Cannot find module 'download-file'
cause Attempting to use ES Modules `import` syntax (`import download from 'download-file'`) in a context where the package is only published as CommonJS.fixUse CommonJS `require` syntax: `const download = require('download-file')`. -
Error: ENOENT: no such file or directory, open './path/to/non-existent/directory/file.jpg'
cause The target directory specified in the `options.directory` did not exist when the download attempted to write the file.fixEnsure the directory exists before initiating the download. Use `fs.mkdirSync(options.directory, { recursive: true });` or `fs.promises.mkdir(options.directory, { recursive: true });`. -
Error: connect ETIMEDOUT
cause The network connection timed out, either due to a slow server, network issues, or the `timeout` option being set too low for a large file or remote server response.fixIncrease the `timeout` option in milliseconds (e.g., `timeout: 60000` for 60 seconds) or check network connectivity and the availability of the remote server. The default timeout is 20000ms (20 seconds).
Warnings
- gotcha The package uses a callback-based API exclusively. It does not provide Promise or async/await support, which is common in modern Node.js development. This can lead to 'callback hell' for complex sequences of downloads.
- gotcha The package does not automatically create the specified `directory` path. If the target directory does not exist, the download will fail with an `ENOENT` error. Developers must manually ensure the directory structure is in place.
- gotcha This package is at a very low version (0.1.5) and appears to be unmaintained. There haven't been updates for a significant period, which could mean it lacks bug fixes, security patches, or compatibility updates for newer Node.js versions or HTTP standards.
- gotcha The package provides basic timeout functionality but lacks advanced features like progress events, stream-based processing for large files, automatic retries on network failures, or integration with `AbortController` for cancellation.
Install
-
npm install download-file -
yarn add download-file -
pnpm add download-file
Imports
- download
import download from 'download-file'
const download = require('download-file') - download (named)
import { download } from 'download-file'const { download } = require('download-file')
Quickstart
const download = require('download-file');
const fs = require('fs');
const path = require('path');
const imageUrl = "https://picsum.photos/id/237/800/600"; // A reliable public image URL
const videoUrl = "https://www.w3schools.com/html/mov_bbb.mp4"; // A reliable public video URL
// --- Example 1: Image download ---
const imageOptions = {
directory: "./downloads/images/",
filename: "random_image.jpg"
};
// Ensure the target directory exists before attempting to download
fs.mkdirSync(imageOptions.directory, { recursive: true });
console.log(`Attempting to download image from ${imageUrl} to ${path.join(imageOptions.directory, imageOptions.filename)}`);
download(imageUrl, imageOptions, function(err){
if (err) {
console.error("Image download failed:", err);
return;
}
console.log("Image 'random_image.jpg' downloaded successfully!");
});
// --- Example 2: Video download with a custom timeout ---
const videoOptions = {
directory: "./downloads/videos/",
filename: "big_buck_bunny.mp4",
timeout: 30000 // 30 seconds, increased for potentially larger files
};
fs.mkdirSync(videoOptions.directory, { recursive: true });
console.log(`Attempting to download video from ${videoUrl} to ${path.join(videoOptions.directory, videoOptions.filename)}`);
download(videoUrl, videoOptions, function(err){
if (err) {
console.error("Video download failed:", err);
return;
}
console.log("Video 'big_buck_bunny.mp4' downloaded successfully!");
});