Registry / devops / fetch-json

fetch-json

JSON →
library3.5.2jsnpmunverified

fetch-json (v3.5.2) is a lightweight JavaScript library (under 4 KB minified) that wraps the Fetch API to reduce boilerplate for JSON REST calls. It automatically sets the content-type header to application/json, runs .json() on responses, serializes request bodies with JSON.stringify(), appends query parameters for GET requests, sets credentials to same-origin, and converts non-JSON text responses to JSON. Released regularly, it supports both browser and Node.js environments, ships TypeScript type definitions, and is ideal for JAMstack architectures. Compared to raw fetch or axios, it provides a simpler API with sensible defaults for JSON endpoints, especially for serverless and client-side apps.

npm install fetch-json
INSTALL
IMPORT
SIG · FETCH-JSON
F
fetch-json
devopsjavascriptv3.5.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.

fetchJson
import { fetchJson } from 'fetch-json';
const fetchJson = require('fetch-json');
fetch-json is ESM-only from v3 onwards; CommonJS require() will fail.
default import
import { fetchJson } from 'fetch-json';
import fetchJson from 'fetch-json';
There is no default export; you must use named import { fetchJson }.
type imports
import type { FetchJsonResponse } from 'fetch-json';
import { FetchJsonResponse } from 'fetch-json';
TypeScript types are exported; use 'import type' for type-only usage.

Demonstrates a basic GET request with query parameters using fetchJson.get().

import { fetchJson } from 'fetch-json'; async function getPlanet() { const url = 'https://api.example.com/planets'; const params = { id: 1 }; try { const data = await fetchJson.get(url, params); console.log('Planet:', data); } catch (error) { console.error('Request failed:', error); } } getPlanet();
Debug
Known issues
breakingFrom v3, fetch-json is ESM-only and does not support CommonJS require().
fix
Use import { fetchJson } from 'fetch-json'; instead of require().
affects: >=3.0.0
breakingIn v3, the library no longer automatically parses response body for HTTP errors as an object; you must handle errors via the callback or catch.
fix
Check data.error in the then() callback or use try/catch with async/await.
affects: >=3.0.0
gotchaThe credentials option defaults to 'same-origin'; for cross-origin requests you may need to set credentials: 'include' or 'omit'.
fix
Pass an options object with credentials: 'include' as third argument to fetchJson.get/post.
affects: >=1.0.0
gotchaIf the response is not valid JSON, fetch-json will throw an error in the catch block; it does not return the raw text.
fix
Wrap call in try/catch or use the error handler in then().
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: fetchJson.get is not a function
Incorrect import: using default import instead of named import.
fix
Change 'import fetchJson from "fetch-json"' to 'import { fetchJson } from "fetch-json"'.
Error [ERR_REQUIRE_ESM]: require() of ES Module not supported
Using CommonJS require() with an ESM-only package.
fix
Use import { fetchJson } from 'fetch-json'; or upgrade to Node.js with ESM support.
SyntaxError: Unexpected token < in JSON at position 0
The server returned HTML (e.g., error page) instead of JSON.
fix
Check the URL or handle the error with try/catch and inspect response status.
Upgrade
Version history
3.5.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
fetch-json — npm install fetch-json · libregistry