{"id":16358,"library":"fetch-to-curl","title":"Convert Fetch HTTP Requests to cURL","description":"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.","status":"active","version":"0.6.0","language":"javascript","source_language":"en","source_url":"https://github.com/leoek/fetch-to-curl","tags":["javascript","curl","fetch","http","request-http","debug","react-native","typescript"],"install":[{"cmd":"npm install fetch-to-curl","lang":"bash","label":"npm"},{"cmd":"yarn add fetch-to-curl","lang":"bash","label":"yarn"},{"cmd":"pnpm add fetch-to-curl","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This library uses named exports, not a default export.","wrong":"import fetchToCurl from 'fetch-to-curl';","symbol":"fetchToCurl","correct":"import { fetchToCurl } from 'fetch-to-curl';"},{"note":"For CommonJS environments, ensure you destructure the named export correctly.","wrong":"const fetchToCurl = require('fetch-to-curl');","symbol":"fetchToCurl (CommonJS)","correct":"const { fetchToCurl } = require('fetch-to-curl');"},{"note":"The library ships with TypeScript types for improved development experience.","symbol":"Type definitions","correct":"import type { FetchToCurlOptions } from 'fetch-to-curl';"}],"quickstart":{"code":"import { fetchToCurl } from 'fetch-to-curl';\n\n// Example 1: Basic URL and options\nconst urlWithOptions = 'https://api.example.com/data';\nconst options = {\n  method: 'POST',\n  headers: {\n    'Content-Type': 'application/json',\n    'Authorization': `Bearer ${process.env.API_TOKEN ?? ''}`\n  },\n  body: JSON.stringify({ key: 'value', id: 123 }),\n};\n\nconsole.log('--- cURL from URL and options ---');\nconsole.log(fetchToCurl(urlWithOptions, options));\n\n// Example 2: Using a Request object\nconst requestObject = new Request('https://api.example.com/users', {\n  method: 'GET',\n  headers: new Headers({\n    'Accept': 'application/xml',\n    'X-Custom-Header': 'Hello'\n  }),\n});\n\nconsole.log('\\n--- cURL from Request object ---');\nconsole.log(fetchToCurl(requestObject));\n\n// Example 3: URL object support (since v0.6.0)\nconst urlObj = new URL('https://example.com/search');\nurlObj.searchParams.set('q', 'test');\nurlObj.searchParams.set('page', '2');\nconst optionsWithUrlObj = { method: 'GET' };\n\nconsole.log('\\n--- cURL from URL object ---');\nconsole.log(fetchToCurl(urlObj, optionsWithUrlObj));","lang":"typescript","description":"Demonstrates converting `fetch` parameters (URL, options, `Request` object, `URL` object) into a cURL command string for debugging."},"warnings":[{"fix":"Upgrade to `fetch-to-curl@0.5.1` or newer to ensure correct escaping of URLs and request bodies.","message":"Prior 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.","severity":"gotcha","affected_versions":"<0.5.1"},{"fix":"Update to `fetch-to-curl@0.6.0` or later for improved handling of header values and support for `URL` objects.","message":"Versions 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.","severity":"gotcha","affected_versions":"<0.6.0"},{"fix":"For optimal TypeScript compatibility and type safety, ensure you are using `fetch-to-curl@0.5.2` or a newer version.","message":"Although 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.","severity":"gotcha","affected_versions":"<0.5.2"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"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.","cause":"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.","error":"TypeError: (0 , fetch_to_curl__WEBPACK_IMPORTED_MODULE_0__.fetchToCurl) is not a function"},{"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\"`).","cause":"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).","error":"ReferenceError: require is not defined"},{"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.","cause":"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.","error":"Output curl command is malformed or doesn't work as expected in terminal."}],"ecosystem":"npm"}