{"id":16440,"library":"miniget","title":"miniget - Small HTTP(S) GET Client","description":"miniget is a lightweight HTTP(S) GET request library designed for both Node.js and browser environments, emphasizing minimal dependencies and a small footprint. It provides core functionalities like automatic redirects (up to 10 by default), request retries for 5xx or connection errors, and reconnects for interrupted downloads, allowing streams to resume. The current stable version is `4.2.3`, with releases typically addressing bug fixes and minor features rather than following a strict semantic versioning cadence between major versions. Key differentiators include its zero-dependency nature, support for streaming responses via a readable stream, and methods for concatenating responses into a single text body. It also exposes a configurable `defaultOptions` object for global settings and ships with TypeScript type definitions, making it well-suited for modern TypeScript projects.","status":"active","version":"4.2.3","language":"javascript","source_language":"en","source_url":"git://github.com/fent/node-miniget","tags":["javascript","request","http","https","redirect","stream","typescript"],"install":[{"cmd":"npm install miniget","lang":"bash","label":"npm"},{"cmd":"yarn add miniget","lang":"bash","label":"yarn"},{"cmd":"pnpm add miniget","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The primary export is a default function. For CommonJS, `const miniget = require('miniget');` works directly.","wrong":"import { miniget } from 'miniget';","symbol":"miniget","correct":"import miniget from 'miniget';"},{"note":"A named export for the custom error class, available since v2.1.0, useful for catching specific miniget errors.","wrong":"import MinigetError from 'miniget';","symbol":"MinigetError","correct":"import { MinigetError } from 'miniget';"},{"note":"A named export for the global default options object, available since v2.1.0, allowing programmatic modification of default request settings.","symbol":"Defaults","correct":"import { Defaults } from 'miniget';"}],"quickstart":{"code":"import miniget from 'miniget';\nimport { createWriteStream } from 'node:fs';\n\nasync function fetchAndStreamData(url: string, filePath: string) {\n  try {\n    // Fetch and concatenate content as text\n    const textBody = await miniget(url).text();\n    console.log(`Fetched text from ${url.substring(0, 30)}... : ${textBody.substring(0, 50)}...`);\n\n    // Stream content to a file\n    const stream = miniget(url, { maxRedirects: 5, maxRetries: 1 });\n    stream.on('redirect', (newUrl) => console.log(`Redirected to: ${newUrl}`));\n    stream.on('error', (err) => console.error(`Stream error: ${err.message}`));\n    stream.on('response', (res) => console.log(`Received status: ${res.statusCode}`));\n\n    const fileStream = createWriteStream(filePath);\n    stream.pipe(fileStream);\n\n    await new Promise<void>((resolve, reject) => {\n      fileStream.on('finish', () => {\n        console.log(`Streamed content to ${filePath}`);\n        resolve();\n      });\n      fileStream.on('error', reject);\n    });\n\n  } catch (error: any) {\n    console.error(`Failed to fetch or stream: ${error.message}`);\n  }\n}\n\n// Example usage with a placeholder URL\n// Replace with a valid URL for actual execution\nconst exampleUrl = 'https://jsonplaceholder.typicode.com/posts/1';\nconst outputFilePath = './output.json';\nfetchAndStreamData(exampleUrl, outputFilePath);\n\n// You can also modify global defaults\nminiget.defaultOptions.headers = { 'User-Agent': 'miniget-example-app' };\nconsole.log('Global User-Agent set.');","lang":"typescript","description":"This quickstart demonstrates both promise-based text retrieval and streaming a response to a file using `miniget`, including error handling and event listeners for redirects and responses. It also shows how to configure global default options."},"warnings":[{"fix":"Replace `stream.on('close', ...)` listeners with `stream.on('end', ...)` for the miniget stream or `fileStream.on('finish', ...)` if piping to a writable stream.","message":"The `close` event is no longer emitted from the miniget stream since v4.0.0. This change was implemented to prevent altering the stream's internal state and ensure proper finishing, aligning with Node.js stream best practices.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Update all calls from `stream.abort()` to `stream.destroy()` to use the current API.","message":"The `abort()` method was deprecated with a runtime warning in v3.0.0 and superseded by the `destroy()` method. The `destroy()` method was introduced to align more closely with Node.js's native HTTP request and stream destruction patterns.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Upgrade to `miniget` v4.2.1 or a newer version to resolve webpack compatibility issues.","message":"Prior to v4.2.1, `miniget` had a bug where it internally required the `url` module, which could cause issues when bundling with webpack for browser environments, leading to build failures or runtime errors.","severity":"gotcha","affected_versions":"<4.2.1"},{"fix":"Ensure your Node.js runtime environment is version 12 or higher to guarantee compatibility and stability.","message":"`miniget` is built for Node.js environments and declares `\"node\": \">=12\"` in its `engines` field. Using it in older Node.js versions (e.g., Node.js 10 or 8) may result in unexpected behavior or runtime errors due to reliance on newer JavaScript features or Node.js APIs.","severity":"gotcha","affected_versions":"<12"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"If working in an ESM environment, use `import miniget from 'miniget';` instead of `const miniget = require('miniget');`.","cause":"Attempting to use `require()` in an ES module (ESM) context, typically indicated by `\"type\": \"module\"` in `package.json` or using `.mjs` file extensions.","error":"ReferenceError: require is not defined"},{"fix":"For ESM, use `import miniget from 'miniget';`. For CommonJS, use `const miniget = require('miniget');`.","cause":"Incorrect import statement. This usually happens when `miniget` is treated as a named export (`import { miniget } from 'miniget';`) when it is a default export, or when attempting an incorrect CommonJS import in an ESM file.","error":"TypeError: miniget is not a function"},{"fix":"Ensure the URL parameter is a correctly formatted string (e.g., `https://example.com/resource`) or a valid `URL` object instance.","cause":"The URL string provided to `miniget` is malformed, not a valid `URL` object, or lacks a proper protocol (e.g., `http://` or `https://`).","error":"Stream error: Invalid URL"},{"fix":"Remove the `stream.on('close', ...)` listener from your code. If you need to detect stream completion, use `stream.on('end', ...)` or monitor the completion of downstream writable streams (e.g., `fileStream.on('finish', ...)`) if you are piping the data.","cause":"Your code is still attempting to listen for the `close` event on the `miniget` stream, which was removed as a breaking change in v4.0.0.","error":"Error: The 'close' event is no longer emitted from the miniget stream."}],"ecosystem":"npm"}