Registry / devops / better-fetch

better-fetch

JSON →
library1.1.2jsnpmunverified

A tiny ES6 fetch() wrapper that simplifies common patterns: automatically sends cookies, allows setting default headers globally, and accepts plain JS objects as request body (auto-converted to FormData). Current stable version is 1.1.2, though the package appears unmaintained since 2016. Compared to alternatives like ky or axios, better-fetch is minimal and stays close to the native fetch API. It is likely compatible with both browser and Node.js environments that support fetch (or with polyfills). The library ships no TypeScript definitions and has no dependencies.

npm install better-fetch
INSTALL
IMPORT
SIG · BETTER-FETCH
B
better-fetch
devopsjavascriptv1.1.2
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.

default (fetch)
import fetch from 'better-fetch'
const { default: fetch } = require('better-fetch')
The package exports a single default function. In CommonJS, use `require('better-fetch')` which returns the function directly.
default (fetch) with commonjs
const fetch = require('better-fetch')
const fetch = require('better-fetch').default
For CommonJS environments, the module exports the function directly, not as a default property.
setDefaultHeaders
fetch.setDefaultHeaders({...})
setDefaultHeaders({...})
setDefaultHeaders is a static method on the default fetch export, not a separate export.

Shows default header setup, GET and POST requests with automatic cookie sending and body-to-FormData conversion.

import fetch from 'better-fetch'; // Set default headers for all requests fetch.setDefaultHeaders({ Authorization: `Token token=${process.env.API_TOKEN ?? ''}`, Accept: 'application/json' }); // GET request fetch('/api/data') .then(response => response.json()) .then(json => { console.log('GET response:', json); }) .catch(err => console.error('GET error:', err)); // POST with plain object body (automatically converted to FormData) const data = { name: 'John', age: 30 }; fetch('/api/users', { method: 'POST', body: data }) .then(response => response.json()) .then(json => { console.log('POST response:', json); }) .catch(err => console.error('POST error:', err));
Debug
Known issues
deprecatedPackage appears unmaintained; last update in 2016 and no recent commits.
fix
Consider migrating to ky, cross-fetch, or native fetch if supported.
affects: all
gotchaAutomatic cookie sending may be blocked by browser SameSite policies in modern browsers.
fix
Ensure the request includes credentials: 'include' if needed, or use native fetch with explicit cookie handling.
affects: all
gotchaUsing body as plain object always converts to FormData. For JSON API calls, you must manually stringify and set Content-Type.
fix
If you need JSON body, pass JSON.stringify(data) and set Content-Type header manually.
affects: all
Errors
Common errors & fixes
require is not defined in ES module scope
Using require in an ESM environment.
fix
Use import fetch from 'better-fetch' instead.
fetch.setDefaultHeaders is not a function
Importing the wrong export (e.g., named import instead of default).
fix
Ensure you use import fetch from 'better-fetch' (default import).
Response is not defined (in Node.js)
Node.js does not have a global Response object by default before v18.
fix
Use a fetch polyfill like node-fetch or cross-fetch.
Upgrade
Version history
1.1.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
6
Amazon
1
Resources
better-fetch — npm install better-fetch · libregistry