Module not found error: Can't resolve '@sanity/client' in Next.js project

5 replies
Last updated: May 29, 2021
Hey everyone, I'm new to all this and I'm sure I'm missing something simple but I'm running into the following on my next+sanity project:
Module not found: Can't resolve '@sanity/client'
> 1 | import sanityClient from '@sanity/client';`
I've been following this
tutorial on the sanity site (more or less)Any help or pointers would be greatly appreciated.
🙂
AI Update

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:

  1. Install the package by running this command in your Next.js project directory:
npm install @sanity/client

or if you're using yarn:

yarn add @sanity/client
  1. 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 apiVersion should 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 createClient function 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-sanity

This 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 thread
5 replies
Is
@sanity/client
listed in
package.json
in the nextjs project folder?
no it's not
you’ll need to run
npm install @sanity/client
(or
yarn add @sanity/client
if you’re using yarn)
inside that folder and it should start working
And that was it! Thanks I knew it was something simple I forgot to do. Appreciate it!
That's strange running that seemed to resolve the error but it doesn't show in
package.json

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.

Was this answer helpful?