Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Issue with client.fetch not being a function in Next.js and Sanity.io integration

7 replies
Last updated: Feb 16, 2023
I'm running some tests for a rather large company considering switching over to sanity. If someone has some time to answer my questions please drop in the replies. Tried a lot of different ways keep getting the same issues about client.fetch not being a function
Feb 16, 2023, 8:50 AM
}wait - compiling...
event - compiled client and server successfully in 1164 ms (1438 modules)
warn - Fast Refresh had to perform a full reload due to a runtime error.
wait - compiling...
event - compiled client and server successfully in 323 ms (1438 modules)
TypeError: _lib_sanity_client__WEBPACK_IMPORTED_MODULE_3__.client.fetch is not a function
at HomePage (webpack-internal:///(sc_server)/./app/(user)/page.tsx
:49:80) at preloadComponent (C
:\Users\brian\OneDrive\Desktop\Fullstack\nextjs-sanity-blog-sonny\node_modules\next\dist\server\app-render.js:86:22) at C
:\Users\brian\OneDrive\Desktop\Fullstack\nextjs-sanity-blog-sonny\node_modules\next\dist\server\app-render.js:908:24 at processTicksAndRejections (node
:internal/process/task_queues:96:5) at async createComponentTree (C
:\Users\brian\OneDrive\Desktop\Fullstack\nextjs-sanity-blog-sonny\node_modules\next\dist\server\app-render.js:907:25) at async C
:\Users\brian\OneDrive\Desktop\Fullstack\nextjs-sanity-blog-sonny\node_modules\next\dist\server\app-render.js:847:56 at async Promise.all (index 0)
at async createComponentTree (C
:\Users\brian\OneDrive\Desktop\Fullstack\nextjs-sanity-blog-sonny\node_modules\next\dist\server\app-render.js:812:38) at async C
:\Users\brian\OneDrive\Desktop\Fullstack\nextjs-sanity-blog-sonny\node_modules\next\dist\server\app-render.js:847:56 at async Promise.all (index 0)
at async createComponentTree (C
:\Users\brian\OneDrive\Desktop\Fullstack\nextjs-sanity-blog-sonny\node_modules\next\dist\server\app-render.js:812:38) at async C
:\Users\brian\OneDrive\Desktop\Fullstack\nextjs-sanity-blog-sonny\node_modules\next\dist\server\app-render.js:1116:51TypeError: _lib_sanity_client__WEBPACK_IMPORTED_MODULE_3__.client.fetch is not a function
at HomePage (webpack-internal:///(sc_server)/./app/(user)/page.tsx
:49:80) at preloadComponent (C
:\Users\brian\OneDrive\Desktop\Fullstack\nextjs-sanity-blog-sonny\node_modules\next\dist\server\app-render.js:86:22) at C
:\Users\brian\OneDrive\Desktop\Fullstack\nextjs-sanity-blog-sonny\node_modules\next\dist\server\app-render.js:908:24 at processTicksAndRejections (node
:internal/process/task_queues:96:5) at async createComponentTree (C
:\Users\brian\OneDrive\Desktop\Fullstack\nextjs-sanity-blog-sonny\node_modules\next\dist\server\app-render.js:907:25) at async C
:\Users\brian\OneDrive\Desktop\Fullstack\nextjs-sanity-blog-sonny\node_modules\next\dist\server\app-render.js:847:56 at async Promise.all (index 0)
at async createComponentTree (C
:\Users\brian\OneDrive\Desktop\Fullstack\nextjs-sanity-blog-sonny\node_modules\next\dist\server\app-render.js:812:38) at async C
:\Users\brian\OneDrive\Desktop\Fullstack\nextjs-sanity-blog-sonny\node_modules\next\dist\server\app-render.js:847:56 at async Promise.all (index 0)
at async createComponentTree (C
:\Users\brian\OneDrive\Desktop\Fullstack\nextjs-sanity-blog-sonny\node_modules\next\dist\server\app-render.js:812:38) at async C
:\Users\brian\OneDrive\Desktop\Fullstack\nextjs-sanity-blog-sonny\node_modules\next\dist\server\app-render.js:1116:51 { digest: '3673357512'
}
Feb 16, 2023, 8:50 AM
in lib
"use client";


import { createClient } from "
user E
/client";

export const projectId = process.env.NEXT_PUBLIC_SANITY_PROJECT_ID;
export const dataset = process.env.NEXT_PUBLIC_SANITY_DATASET;
export const apiVersion = process.env.NEXT_PUBLIC_SANITY_API_VERSION;

export const client = createClient({ projectId, dataset, apiVersion });
Feb 16, 2023, 8:51 AM
alos used import { createClient } from "next-sanity"; nothing
Feb 16, 2023, 8:52 AM
import { Inter } from "@next/font/google";
import { previewData } from "next/headers";
import { groq } from "next-sanity";
import PreviewSuspense from "components/PreviewSuspense";
import { client } from "../../lib/sanity.client";
const query = groq`
*[_type=="post"]{
...,
author->,
categories[]->,
} | order(_createdAt desc)
`;


// const inter = Inter({ subsets: ["latin"] });

export default async function HomePage() {
if (previewData()) {
return ( <PreviewSuspense

fallback={ <div
role="status"> <p
className="text-center text-lg animate-pusle text-[#fff]"> Loading Preview Data
</p>
</div>
}
>
<div>1</div>
</PreviewSuspense>
);
}

const posts =
await client.fetch(query);

// console.warn(data);
return ( <div
className="bg-white h-screen text-black text-4xl"> {
/* Bloglists */} <h1
className="">{posts}</h1> </div>
);
}
Feb 16, 2023, 8:52 AM
pretty popular build on line everything up to this point has been no issue. Please offer some pointers if you have come across this issue before
Feb 16, 2023, 8:52 AM
m install next-sanity @portabletext/react @sanity/image-url

npm ERR! code ERESOLVE
npm ERR! ERESOLVE could not resolve
npm ERR!
npm ERR! While resolving: @sanity/vision@3.0.0
npm ERR! Found: @sanity/client@5.2.1
npm ERR! node_modules/@sanity/client
npm ERR! @sanity/client@"^5.2.1" from the root project
npm ERR! @sanity/client@"^5" from next-sanity@4.1.5
npm ERR! node_modules/next-sanity
npm ERR! next-sanity@"^4.1.3" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer @sanity/client@"^3.4.1" from @sanity/vision@3.0.0
npm ERR! node_modules/@sanity/vision
npm ERR! @sanity/vision@"3.0.0" from the root project
npm ERR!
npm ERR! Conflicting peer dependency: @sanity/client@3.4.1
npm ERR! node_modules/@sanity/client
npm ERR! peer @sanity/client@"^3.4.1" from @sanity/vision@3.0.0
npm ERR! node_modules/@sanity/vision
npm ERR! @sanity/vision@"3.0.0" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR!
npm ERR! See C:\Users\brian\AppData\Local\npm-cache\eresolve-report.txt for a full report.
Feb 16, 2023, 8:54 AM
... hmmm. Worked it out, kinda dumb issue.. Had to use the cache..
Feb 16, 2023, 9:36 AM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?