Registry / testing / is-network-error

is-network-error

JSON →
library1.3.2jsnpmunverified

Detect Fetch network errors across Node.js, Bun, Deno, and browsers. v1.3.2 – stable, low release cadence. Unlike manual checks (e.g., error.message.includes('fetch')), it handles the non-standardized error differences across runtimes. Useful for conditional retry logic or fallback handling in fetch-based code. Written in TypeScript, ships type definitions. Requires Node >=16. Minimal, zero-dependency package.

npm install is-network-error
INSTALL
IMPORT
SIG · IS-NETWORK-ERROR
I
is-network-error
testingjavascriptv1.3.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.

isNetworkError
import isNetworkError from 'is-network-error'
const isNetworkError = require('is-network-error')
ESM-only package; CJS require() will fail. Use dynamic import() if needed in CJS.
isNetworkError (type import)
import type isNetworkError from 'is-network-error'
import { isNetworkError } from 'is-network-error'
Named import is incorrect because the package exports a default function only. Type import is optional–you can just import the value.
isNetworkError (dynamic import)
const { default: isNetworkError } = await import('is-network-error')
const isNetworkError = await import('is-network-error')
Dynamic import returns a module object; the default is on .default.

Shows how to use isNetworkError to distinguish fetch network errors from other errors and implement fallback logic.

import isNetworkError from 'is-network-error'; async function fetchWithFallback() { try { const res = await fetch('https://example.com/data.json'); return res.json(); } catch (error) { if (isNetworkError(error)) { // Network error, use fallback console.log('Network error, using cached data'); return { fallback: true }; } throw error; // Re-throw non-network errors } } fetchWithFallback().then(console.log).catch(console.error);
Debug
Known issues
breakingv1.0.0 dropped CJS support; package is ESM-only. Node.js <16 not supported.
fix
Use Node >=16 and ESM imports, or use dynamic import() in CJS.
affects: >=1.0.0
gotchaThe function checks for Fetch network errors only; it will return false for other TypeError instances (e.g., JSON.parse errors).
fix
Ensure you only pass errors from fetch() calls.
affects: >=0.1.0
deprecatedVersions <1.0.0 used a different API (e.g., named exports). Upgrade to v1 for default export.
fix
Update import to default import: `import isNetworkError from 'is-network-error'`
affects: <1.0.0
Errors
Common errors & fixes
SyntaxError: Cannot use import statement outside a module
Using ESM import in a CommonJS (CJS) file without enabling ESM.
fix
Add 'type': 'module' in your package.json or rename the file to .mjs, or use dynamic import(): `const { default: isNetworkError } = await import('is-network-error')`
ERR_REQUIRE_ESM: require() of ES Module /path/to/node_modules/is-network-error/index.js from /path/to/project/index.js not supported.
Attempting to require() an ESM-only package.
fix
Use dynamic import() or switch to ESM in your project.
TypeError: isNetworkError is not a function
Named import instead of default import.
fix
Use `import isNetworkError from 'is-network-error'` (default import) not `import { isNetworkError } from 'is-network-error'`.
Upgrade
Version history
1.3.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
15 hits · last 30 days
node
14
Resources
is-network-error — npm install is-network-error · libregistry