Module not found error: Can't resolve '@sanity/client' in Next.js project
I can see you're getting a "Module not found: Can't resolve '@sanity/client'" error. This is a common issue that's easy to fix!
The problem is that you haven't installed the @sanity/client package in your Next.js project. When following older tutorials, the import syntax has changed slightly, but the main issue here is the missing dependency.
Here's how to fix it:
- Install the package by running this command in your Next.js project directory:
npm install @sanity/clientor if you're using yarn:
yarn add @sanity/client- Update your import syntax - Modern Sanity uses a slightly different import. Instead of:
import sanityClient from '@sanity/client';Use this:
import { createClient } from '@sanity/client';
export const client = createClient({
projectId: 'your-project-id',
dataset: 'production',
apiVersion: '2023-06-10', // use current date (YYYY-MM-DD format)
useCdn: true,
});A few important notes:
- Make sure you replace
'your-project-id'with your actual Sanity project ID (you can find this in your Sanity management dashboard) - The
apiVersionshould be set to today's date or the date you're developing (format: YYYY-MM-DD) - The tutorial you're following is a year old, so some syntax has changed - the
createClientfunction is now the standard way to initialize the client
If you're using the newer Sanity setup, you might also want to consider using the next-sanity package instead, which provides better integration with Next.js. You can install it with:
npm install next-sanityThis package includes @sanity/client and provides additional utilities specifically for Next.js projects. The Sanity + Next.js documentation has great examples of modern setups!
Show original thread5 replies
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.