Search code examples
next.jssanity

Server Error: Configuration must contain `projectId`


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.


Solution

  • 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.