Registry / devops / create-fetch

create-fetch

JSON →
library3.3.1jsnpmunverified

A lightweight (1.4KB gzip) utility library for composing custom fetch wrappers with reusable middleware. Current stable version is 3.3.1, released regularly with a focus on TypeScript support and ESM compatibility. Differentiates from other fetch wrappers by providing a simple compose-based API (composeFetch) or pipeline pattern, alongside built-in middleware for default options, base URL injection, headers, query string serialization, JSON body serialization, and XSRF token handling. Supports both CommonJS and ESM, and ships TypeScript definitions.

npm install create-fetch
INSTALL
IMPORT
SIG · CREATE-FETCH
C
create-fetch
devopsjavascriptv3.3.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

composeFetch
import { composeFetch } from 'create-fetch'
const composeFetch = require('create-fetch').composeFetch
Module is ESM by default; CJS require() works but is not recommended due to tree-shaking issues.
query
import { query } from 'create-fetch'
import query from 'create-fetch'
query is a named export, not default.
FetchEnhancer
import type { FetchEnhancer } from 'create-fetch'
import { FetchEnhancer } from 'create-fetch' (at runtime)
FetchEnhancer is a TypeScript type; use `import type` for correct TypeScript compilation.

Shows how to create a custom fetch with composed middleware including query string, default headers, and JSON body serialization.

import 'cross-fetch/polyfill'; // only in Node import { composeFetch, query, headers, bodify } from 'create-fetch'; const myFetch = composeFetch([ query(), headers({ 'x-requested-with': 'fetch' }), bodify(), ])(fetch); async function run() { const response = await myFetch('/api/data', { method: 'POST', query: { filter: 'user' }, body: { name: 'JoJo' }, }); console.log(await response.json()); } run().catch(console.error);
Debug
Known issues
gotchaThe `query()` middleware does not merge with existing query string in the URL; it overrides it.
fix
If you need to preserve existing query params, manually parse the URL before calling fetch.
affects: >=1.0
deprecatedThe default export `createFetch` is deprecated as of v3; use `composeFetch` instead.
fix
Replace default import with named import `composeFetch`.
affects: >=3.0.0
breakingIn v2, middleware order was reversed; v3 changed to left-to-right order.
fix
Ensure middleware are arranged in application order now: first middleware wraps the innermost fetch.
affects: >=3.0.0
gotchaThe `bodify()` middleware sets Content-Type to application/json only if the body is a plain object, not if it's a FormData or URLSearchParams.
fix
Manually set Content-Type header for multipart forms or use browser's default for FormData.
affects: >=1.0
Errors
Common errors & fixes
Error: fetch is not defined
Running in Node.js without a fetch polyfill.
fix
Install and import a polyfill like 'cross-fetch/polyfill' at the entry point.
TypeError: (0 , _createFetch.composeFetch) is not a function
Using CommonJS require() incorrectly on an ESM module.
fix
Use ES module import syntax: `import { composeFetch } from 'create-fetch'`.
Default import 'createFetch' is not a function
Trying to use the deprecated default export that was removed in v3.
fix
Replace `import createFetch from 'create-fetch'` with `import { composeFetch } from 'create-fetch'`.
Upgrade
Version history
3.3.1latest on npm
Audit
Dependencies
cross-fetchoptionalPolyfill for fetch in Node.js environments; not always required but commonly used.
Agent activity
4 hits · last 30 days
node
4
Resources
create-fetch — npm install create-fetch · libregistry