sync-fetch is a JavaScript library that provides a synchronous wrapper around the standard Fetch API, enabling blocking network requests in environments where asynchronicity is not desired or feasible. Currently at version 0.6.0, it sees intermittent updates, with the last publish approximately four months ago, indicating an actively maintained but not rapidly evolving project. It internally leverages `node-fetch` for Node.js environments and `XMLHttpRequest` for browser contexts, adapting the underlying mechanism to provide a unified synchronous interface. Its primary differentiation is the synchronous execution, which, while useful for niche scenarios such as initial configuration loading or specific command-line utilities in Node.js, or within Web Workers in browsers to avoid blocking the main thread, generally carries significant performance and responsiveness implications due to its blocking nature. It is crucial to understand that using `sync-fetch` on the main thread of a browser or within the Node.js event loop will halt all other operations until the network request completes, making it generally unsuitable for interactive applications. The library explicitly outlines several limitations, particularly regarding body types like `Stream` or `Blob` and some advanced `fetch` options, which are inherent to synchronous request models.
npm install sync-fetchVerified import paths — ran on the pinned version, not inferred.
Demonstrates a basic synchronous GET request and synchronously parses a JSON response, followed by a text response and an example using an environment variable for authentication.
For browser applications, use standard, asynchronous `fetch` or ensure `sync-fetch` is only used within a Web Worker. For Node.js, prefer asynchronous I/O with `node-fetch` or other promise-based libraries unless a blocking operation is strictly required for a specific, isolated use case (e.g., a CLI tool's initial setup).
For Node.js, ensure request bodies are JSON, strings, or `Buffer` objects that can be serialized synchronously. Avoid using `Stream`, `Blob`, or `FormData` directly as body content.
Be aware of reduced functionality in browsers. Test thoroughly for desired behavior, especially concerning CORS. For advanced `fetch` options, consider asynchronous `fetch`.
If encountering `ERR_REQUIRE_ESM` when using `sync-fetch` in a CJS project, verify your Node.js version and environment. Ensure `sync-fetch`'s internal handling of `node-fetch` v3 is compatible, or consider alternative synchronous HTTP clients if issues persist.
Convert `Stream` or `Blob` data into a synchronously readable format (e.g., `Buffer` or string) before passing it as the `body` option.
For Node.js, ensure you are using `const fetch = require('sync-fetch')`. If the error persists due to internal dependencies, verify your Node.js runtime environment (>=18 is required). If you are in an ESM project, and `sync-fetch` does not provide an ESM export, you might need to use dynamic `import()` or tools that bridge CJS and ESM, though this package's core design revolves around synchronous CJS for Node.js.Refactor your code to use the standard, asynchronous `fetch` API for browser main thread operations. If synchronous behavior is absolutely required, move the `sync-fetch` call into a Web Worker to avoid blocking the UI thread.