{"id":17206,"library":"digest-fetch","title":"Digest Fetch","description":"digest-fetch is a JavaScript/TypeScript library that provides digest and basic HTTP authentication capabilities for both the standard `fetch` API and `node-fetch` in Node.js environments. The current stable version is 3.1.1. This library distinguishes itself by strictly adhering to RFC2069, RFC2617, and RFC7616 for digest access authentication, supporting various algorithms like MD5, SHA-256, and SHA-512-256, including their session variants. It primarily acts as a plugin, wrapping the `fetch` function to handle the authentication challenge-response cycle seamlessly. Major version 3.x transitioned the package to an ES module, requiring changes in project configuration for both JavaScript and TypeScript users. It allows for customizable options such as algorithm, status codes for failure, and cnonce size, and can also be configured to perform basic HTTP authentication.","status":"active","version":"3.1.1","language":"javascript","source_language":"en","source_url":"https://github.com/devfans/digest-fetch","tags":["javascript","digest","auth","fetch","node-fetch","http","basic","authentication","typescript"],"install":[{"cmd":"npm install digest-fetch","lang":"bash","label":"npm"},{"cmd":"yarn add digest-fetch","lang":"bash","label":"yarn"},{"cmd":"pnpm add digest-fetch","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Provides the fetch API implementation for Node.js environments; required for usage outside of browsers.","package":"node-fetch","optional":false}],"imports":[{"note":"Since v3.0.0, digest-fetch is an ES Module. Use `import` and ensure your project is configured for ESM ('type': 'module' in package.json).","wrong":"const DigestClient = require('digest-fetch');","symbol":"DigestClient","correct":"import DigestClient from 'digest-fetch';"},{"note":"For digest-fetch v2.0.3 or below, CommonJS `require` is the correct way. V3+ is ESM-only.","wrong":"import DigestClient from 'digest-fetch';","symbol":"DigestClient (CJS)","correct":"const DigestClient = require('digest-fetch');"},{"note":"DigestClient is a class, so it's imported as a value. Types are shipped with the package since v3, so no `@types/digest-fetch` is needed.","wrong":"import type { DigestClient } from 'digest-fetch';","symbol":"DigestClient (TypeScript)","correct":"import DigestClient from 'digest-fetch';"}],"quickstart":{"code":"import DigestClient from 'digest-fetch';\nimport fetch from 'node-fetch'; // Required for Node.js environments\n\n// Ensure node-fetch is globally available for DigestClient\n// @ts-ignore - this is a common pattern for polyfilling fetch\nglobalThis.fetch = fetch;\n\nasync function authenticateAndFetch() {\n  // Replace with actual credentials and URL\n  const username = process.env.DIGEST_USER ?? 'testuser';\n  const password = process.env.DIGEST_PASS ?? 'testpassword';\n  const url = 'http://httpbin.org/digest-auth/auth/testuser/testpassword/MD5'; \n\n  // Create a DigestClient instance\n  const client = new DigestClient(username, password, { algorithm: 'MD5' });\n\n  try {\n    // Make a request using the client's fetch method\n    const response = await client.fetch(url, { method: 'GET' });\n\n    if (!response.ok) {\n      console.error(`HTTP error! status: ${response.status}`);\n      const errorText = await response.text();\n      console.error('Error details:', errorText);\n      return;\n    }\n\n    const data = await response.json();\n    console.log('Successfully authenticated and fetched data:');\n    console.dir(data);\n  } catch (error) {\n    console.error('An error occurred:', error);\n  }\n}\n\nauthenticateAndFetch();","lang":"typescript","description":"This quickstart demonstrates how to set up `digest-fetch` for digest authentication, create a `DigestClient`, and make an authenticated request in a TypeScript Node.js environment."},"warnings":[{"fix":"For Node.js projects, add `\"type\": \"module\"` to your `package.json`. For TypeScript, ensure `tsconfig.json` includes `\"module\": \"ESNext\"` and `\"moduleResolution\": \"node\"`.","message":"Version 3.0.0 and above of `digest-fetch` are ES Module (ESM) only. This requires projects to be configured for ESM, including setting `\"type\": \"module\"` in `package.json` for Node.js projects.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Update `node-fetch` to its latest version (`npm install node-fetch@latest`). Review `node-fetch` v3 upgrade guides for its specific breaking changes, such as the removal of the `timeout` option.","message":"When migrating to `digest-fetch` v3+, if you were previously using `node-fetch` v2, you will also need to upgrade `node-fetch` to v3+. `node-fetch` v3+ is also ESM-only, which aligns with `digest-fetch` v3+ but introduces its own breaking changes.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"The library suggests using a custom alternate authentication failure code (`statusCode` option) to avoid the browser prompt. Alternatively, ensure the server returns a different status for initial challenges or handle the 401 in a way that doesn't trigger the browser UI.","message":"When using digest authentication in browsers, a 401 response might trigger a browser's native authentication prompt, which is often undesirable for programmatic use.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Ensure your project is configured as an ES module by adding `\"type\": \"module\"` to your `package.json`, and use `import DigestClient from 'digest-fetch';`. Alternatively, downgrade `digest-fetch` to a v2.x version if your project must remain CommonJS.","cause":"Attempting to use CommonJS `require()` in an ES Module (ESM) context for `digest-fetch` v3+.","error":"ReferenceError: require is not defined"},{"fix":"Ensure your project's `package.json` has `\"type\": \"module\"`. If you intend to use CommonJS, you must downgrade `digest-fetch` to v2.x.","cause":"Attempting to use ES Module `import` syntax in a CommonJS context without proper configuration for `digest-fetch` v3+.","error":"SyntaxError: Cannot use import statement outside a module"},{"fix":"Install `node-fetch` (`npm install node-fetch`) and ensure it's made available globally, for instance, by adding `globalThis.fetch = fetch;` after importing `node-fetch` in your entry file.","cause":"In Node.js environments, `digest-fetch` relies on a global `fetch` implementation, typically provided by `node-fetch`, which has not been correctly imported or polyfilled.","error":"TypeError: fetch is not a function"}],"ecosystem":"npm","meta_description":null}