Registry / devops / cross-fetch-json

cross-fetch-json

JSON →
library2.0.0jsnpmunverified

Isomorphic fetch wrapper that automatically parses JSON responses. Version 2.0.0 wraps cross-fetch and adds Zod integration via getFetchFn. It returns JSONValue or undefined, and supports custom schemas. Compared to raw fetch, it eliminates boilerplate for JSON parsing and error handling. Maintained by bconnorwhite, with regular updates.

npm install cross-fetch-json
INSTALL
IMPORT
SIG · CROSS-FETCH-JSON
C
cross-fetch-json
devopsjavascriptv2.0.0
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 'cross-fetch-json'
const fetchJSON = require('cross-fetch-json')
ESM-only since v2
getFetchFn
import { getFetchFn } from 'cross-fetch-json'
import { getFetchFn } from 'cross-fetch-json/getFetchFn'
Import from package root, not subpath
JSONValue
import { JSONValue } from 'cross-fetch-json'
import { JSONValue } from 'types-json'
Type exported from this package
JSONObject
import type { JSONObject } from 'cross-fetch-json'
import { JSONObject } from 'cross-fetch-json'
Use type import for TypeScript if not needed at runtime

Demonstrates basic fetchJSON usage and typed fetch with Zod schema via getFetchFn.

import { fetchJSON, getFetchFn } from 'cross-fetch-json'; import { z } from 'zod'; async function example() { const data = await fetchJSON('https://api.example.com/data'); console.log(data); } const userSchema = z.object({ name: z.string(), age: z.number() }); const fetchUser = getFetchFn(userSchema); async function typedExample() { const user = await fetchUser('https://api.example.com/user'); if (user) { console.log(user.name); } } example(); typedExample();
Debug
Known issues
gotchafetchJSON returns undefined for non-JSON responses
fix
Check response type or use getFetchFn with a schema to ensure non-null.
affects: >=1.0.0
gotchaWhen using getFetchFn, the Zod schema must parse the entire JSON object
fix
Design schema to accept the full response shape, or use .passthrough() for unknown keys.
affects: >=2.0.0
breakingv2 migrated from 'node-fetch' to 'cross-fetch' for wider platform support
fix
Update imports; use same API but check cross-fetch documentation for edge cases.
affects: >=2.0.0
deprecatedThe default export fetchJSON was renamed to named export in v2
fix
Change import to named: import { fetchJSON } from 'cross-fetch-json'.
affects: <2.0.0
Errors
Common errors & fixes
Cannot find module 'cross-fetch-json'
Package not installed or module resolution error
fix
Run 'npm install cross-fetch-json' or ensure your bundler resolves node_modules.
The requested module 'cross-fetch-json' does not provide an export named 'default'
Using default import in v2 where only named exports exist
fix
Change to named import: import { fetchJSON } from 'cross-fetch-json'.
Property 'name' does not exist on type 'JSONValue | undefined'
fetchJSON returns union type; TypeScript requires narrowing
fix
Add type guard or use getFetchFn with a Zod schema.
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies
cross-fetchrequiredProvides the underlying fetch implementation
parse-json-objectrequiredParses JSON into typed objects
zodoptionalUsed by getFetchFn for schema validation
Agent activity
5 hits · last 30 days
node
4
Amazon
1
Resources
cross-fetch-json — npm install cross-fetch-json · libregistry