Registry / devops / next-fetch

next-fetch

JSON →
library1.0.1jsnpmunverified

A lightweight fetch wrapper for Next.js that transparently passes cookies and CSRF tokens between server-side and client-side requests. Version 1.0.1 is the latest stable release. Unlike raw unfetch or isomorphic-fetch, it integrates req/res objects from Next.js context to maintain session consistency during SSR. Requires manual server-side configuration to copy set-cookie headers. No active development since 2018.

npm install next-fetch
INSTALL
IMPORT
SIG · NEXT-FETCH
N
next-fetch
devopsjavascriptv1.0.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.

getFetch
const getFetch = require('next-fetch')
import getFetch from 'next-fetch'
Package uses CommonJS; ESM import may fail.
getFetch
const getFetch = require('next-fetch'); const fetch = getFetch()
const fetch = require('next-fetch')
getFetch is a factory function; you must call it to obtain the fetch function.
fetch
const { req, res } = context; fetch(url, options, req, res)
fetch(url, options, context)
Third and fourth arguments must be the specific req and res objects from Next.js context, not the entire context object.

Shows how to use next-fetch in Next.js getInitialProps with req/res context.

const getFetch = require('next-fetch'); const fetch = getFetch(); Page.getInitialProps = async ({ req, res }) => { try { const response = await fetch('http://localhost:3000/api/user', {}, req, res); const user = await response.json(); return { user }; } catch (error) { return { error: error.message }; } };
Debug
Known issues
breakingPackage is no longer maintained; may break with newer Next.js versions (especially after Next.js 9.3).
fix
Consider using native fetch or axios with manual cookie forwarding.
affects: >=1.0.1
gotchaYou must manually copy set-cookie header to custom-set-cookie on the server side.
fix
Add middleware to copy res.getHeader('set-cookie') to res.header('custom-set-cookie', ...).
affects: *
deprecatedUses Next.js legacy getInitialProps pattern; not compatible with getServerSideProps or App Router.
fix
Migrate to getServerSideProps and handle cookies via req.headers.cookie.
affects: *
gotchagetFetch must be called with a config object to override default header names.
fix
Example: getFetch({ csrftokenHeaderName: 'x-csrf-token' }).
affects: *
Errors
Common errors & fixes
TypeError: fetch is not a function
Importing next-fetch directly as fetch without calling getFetch.
fix
const fetch = require('next-fetch')();
ReferenceError: req is not defined
Trying to use fetch without providing req and res arguments.
fix
Always pass req and res as third and fourth arguments: fetch(url, options, req, res).
Cannot find module 'next-fetch'
Package not installed or typo in require.
fix
Run: npm install next-fetch
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies
unfetchrequiredcore fetch polyfill used internally
Agent activity
4 hits · last 30 days
node
4
Resources
next-fetch — npm install next-fetch · libregistry