0

I have a local repo of a live NextJS/Sanity website, but I've set the Sanity projectID to a different blank Sanity project to avoid overwriting the live website.

At http://localhost:3000/, I'm receiving:

Server Error
Error: Configuration must contain `projectId`

lib\client.js contains:

const config = {
  projectId: process.env.NEXT_PUBLIC_SANITY_ID || 'xxxxx',
  dataset: process.env.NEXT_PUBLIC_SANITY_DATASET || 'production',
  apiVersion: '2021-03-25',
  useCdn: true,
}

const client = sanityClient(config)

I've added a CORS origin to my project & there's also a .env.local.example in the root folder

NEXT_PUBLIC_SANITY_PROJECT_ID="xxxxxx"
NEXT_PUBLIC_PROJECT_ID="sj830cdx"

(so not solved by this answer)

If I rename env.local.example to env.local http://localhost:3000/ will load blank.

Help appreciated.

Steve
  • 2,066
  • 13
  • 60
  • 115
  • Does this answer your question? [Uncaught Error: Configuration must contain \`projectId\`( when using environment variable )](https://stackoverflow.com/questions/71451219/uncaught-error-configuration-must-contain-projectid-when-using-environment-v) – Yilmaz Feb 27 '23 at 00:30
  • @Yilmaz, no, I've added a CORS origin to my project and my `.env.local` is in the root folder. – Steve Feb 27 '23 at 01:04

2 Answers2

0

Seems like the error message you received indicates that the projectId configuration value is missing in the config object used to initialize the sanityClient. The projectId is a required configuration parameter for the Sanity client to work properly. To fix the issue, you can update the config object in lib/client.js to use NEXT_PUBLIC_SANITY_PROJECT_ID

const config = {
  projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID || 'xxxxx',
  dataset: process.env.NEXT_PUBLIC_SANITY_DATASET || 'production',
  apiVersion: '2021-03-25',
  useCdn: true,
}

Then, make sure to rename your .env.local.example file back to .env.local and set the value of NEXT_PUBLIC_SANITY_PROJECT_ID to the correct project ID for your Sanity project. This should allow the sanityClient to initialize correctly and resolve the server error you were seeing.

  • Thanks. I've changed `NEXT_PUBLIC_SANITY_ID` to `NEXT_PUBLIC_SANITY_PROJECT_ID`. I still receive a blank page. I think it's because my Sanity project contains no content. I'm having problems with [npm run build](https://stackoverflow.com/questions/75575749/nextjs-sanity-error-failed-to-collect-page-data) which I need to solve first. – Steve Feb 26 '23 at 23:56
  • 1
    I've cloned the repo again and kept the original project ID. Working now. Thanks. – Steve Feb 27 '23 at 02:01
0

I changed the sanity import to as below and it worked for me. import {createClient) from 'next-sanity'; Also I read that the sanityClient configuration is deprecated. Use createClient instead.

  • Welcome to Stack Overflow, Bryan. When you add code to a question or answer, make sure you enclose it in backticks, and it will be formatted as code when you click Save. – Steve Apr 18 '23 at 10:47
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 24 '23 at 20:16