Registry / http-networking / fetch-to-curl

fetch-to-curl

JSON →
library0.6.0jsnpmunverified

fetch-to-curl is a JavaScript/TypeScript utility library that generates `curl` commands from standard Web Fetch API inputs, including URLs, `Request` objects, and `RequestInit` options. Currently at version 0.6.0, it aims for simplicity and minimalism with zero external dependencies. Unlike libraries that patch the global `fetch` object, fetch-to-curl operates as a standalone wrapper, ensuring no side effects on your application's actual network requests. Releases are made periodically to address bug fixes and add minor enhancements, as seen with recent updates supporting URL objects and improving escaping. It is differentiated by its lightweight nature and its direct translation approach, making it ideal for debugging HTTP requests in development environments without modifying the runtime behavior of the application.

npm install fetch-to-curl
INSTALL
IMPORT
SIG · FETCH-TO-CURL
F
fetch-to-curl
http-networkingjavascriptv0.6.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

fetchToCurl
import { fetchToCurl } from 'fetch-to-curl';
import fetchToCurl from 'fetch-to-curl';
This library uses named exports, not a default export.
fetchToCurl (CommonJS)
const { fetchToCurl } = require('fetch-to-curl');
const fetchToCurl = require('fetch-to-curl');
For CommonJS environments, ensure you destructure the named export correctly.
Type definitions
import type { FetchToCurlOptions } from 'fetch-to-curl';
The library ships with TypeScript types for improved development experience.

Demonstrates converting `fetch` parameters (URL, options, `Request` object, `URL` object) into a cURL command string for debugging.

import { fetchToCurl } from 'fetch-to-curl'; // Example 1: Basic URL and options const urlWithOptions = 'https://api.example.com/data'; const options = { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${process.env.API_TOKEN ?? ''}` }, body: JSON.stringify({ key: 'value', id: 123 }), }; console.log('--- cURL from URL and options ---'); console.log(fetchToCurl(urlWithOptions, options)); // Example 2: Using a Request object const requestObject = new Request('https://api.example.com/users', { method: 'GET', headers: new Headers({ 'Accept': 'application/xml', 'X-Custom-Header': 'Hello' }), }); console.log('\n--- cURL from Request object ---'); console.log(fetchToCurl(requestObject)); // Example 3: URL object support (since v0.6.0) const urlObj = new URL('https://example.com/search'); urlObj.searchParams.set('q', 'test'); urlObj.searchParams.set('page', '2'); const optionsWithUrlObj = { method: 'GET' }; console.log('\n--- cURL from URL object ---'); console.log(fetchToCurl(urlObj, optionsWithUrlObj));
Debug
Known issues
gotchaPrior to version 0.5.1, `fetch-to-curl` might generate `curl` strings with improperly escaped URLs or request bodies, especially for strings containing single quotes or special characters. This could lead to malformed commands when executed in a terminal.
fix
Upgrade to `fetch-to-curl@0.5.1` or newer to ensure correct escaping of URLs and request bodies.
affects: <0.5.1
gotchaVersions older than 0.6.0 had issues with missing single quote escaping for certain header values and did not correctly handle `null` or `undefined` headers, potentially leading to incorrect cURL output or errors.
fix
Update to `fetch-to-curl@0.6.0` or later for improved handling of header values and support for `URL` objects.
affects: <0.6.0
gotchaAlthough TypeScript types were improved in v0.5.2, relying on older versions in a TypeScript project might result in less accurate type inference or require more manual type assertions.
fix
For optimal TypeScript compatibility and type safety, ensure you are using `fetch-to-curl@0.5.2` or a newer version.
affects: <0.5.2
Errors
Common errors & fixes
TypeError: (0 , fetch_to_curl__WEBPACK_IMPORTED_MODULE_0__.fetchToCurl) is not a function
This error typically occurs in environments like Webpack or other bundlers when using an incorrect import syntax for a named export, often trying to import it as a default export or destructing incorrectly in a CJS context.
fix
Ensure you are using named import syntax: `import { fetchToCurl } from 'fetch-to-curl';` for ESM, or `const { fetchToCurl } = require('fetch-to-curl');` for CommonJS. Do not use `import fetchToCurl from 'fetch-to-curl';` as it is not a default export.
ReferenceError: require is not defined
You are attempting to use CommonJS `require()` syntax in an ECMAScript Module (ESM) file (indicated by `"type": "module"` in `package.json` or a `.mjs` extension).
fix
Change your import statement to use ESM syntax: `import { fetchToCurl } from 'fetch-to-curl';`. If you must use CommonJS, ensure your file is treated as CJS (e.g., `.js` extension without `"type": "module"`).
Output curl command is malformed or doesn't work as expected in terminal.
This can happen if you are using an older version of `fetch-to-curl` that had issues with character escaping in URLs or request bodies, or improper handling of certain header values.
fix
Upgrade `fetch-to-curl` to the latest version (`0.6.0` or newer) to benefit from fixes related to single quote escaping, URL object support, and robust handling of `null`/`undefined` headers. Always test the generated curl command after an upgrade.
Upgrade
Version history
0.6.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
fetch-to-curl — npm install fetch-to-curl · libregistry