admin管理员组

文章数量:1287583

I have tried everything to fix it but still getting the error

Uncaught error: Missing environment variable: NEXT_PUBLIC_SANITY_DATASET
http://localhost:3333/static/sanity-5377bc10.js:4605:43956
Error: Missing environment variable: NEXT_PUBLIC_SANITY_DATASET
    at Wke (http://localhost:3333/static/sanity-5377bc10.js:4605:43956)
    at http://localhost:3333/static/sanity-5377bc10.js:4605:43720

I have tried everything to fix it but still getting the error

Uncaught error: Missing environment variable: NEXT_PUBLIC_SANITY_DATASET
http://localhost:3333/static/sanity-5377bc10.js:4605:43956
Error: Missing environment variable: NEXT_PUBLIC_SANITY_DATASET
    at Wke (http://localhost:3333/static/sanity-5377bc10.js:4605:43956)
    at http://localhost:3333/static/sanity-5377bc10.js:4605:43720

Share Improve this question edited May 19, 2023 at 14:35 TylerH 21.1k77 gold badges79 silver badges112 bronze badges asked May 19, 2023 at 10:12 I byteI byte 511 silver badge3 bronze badges 2
  • 1 Where/when are you getting this error? Are you installing Sanity? Using it? Running some code somewhere? See minimal reproducible example for more info on how you can improve your question so it is more likely to get an answer. – TylerH Commented May 19, 2023 at 14:36
  • no error in the code its was after I ran sanity start and clicked the server link – I byte Commented May 24, 2023 at 9:00
Add a ment  | 

6 Answers 6

Reset to default 5

You must use the SANITY_STUDIO_ prefix, not the NEXT_PUBLIC_ prefix.

See Sanity.io documentation on env variables

To fix your error:

  1. Replace mentions of NEXT_PUBLIC_SANITY_DATASET with SANITY_STUDIO_DATASET

  2. Ensure SANITY_STUDIO_DATASET is set with correct project ID in your environment

literally and I think this is silly but it worked for me. Change the naming convention in your .env or .env.local file (also dont forget to change everything referencing it also aswell) and also move the env file to the sanity root folder if you're still having issues

before NEXT_PUBLIC_SANITY_PROJECT_ID="your ID" NEXT_PUBLIC_SANITY_DATASET="your dataset"

after SANITY_STUDIO_PROJECT_ID="your ID" SANITY_STUDIO_DATASET="your Dataset"

this is due to the changes in the way the sanity environment has changed to look for env variables. but when you install sanity it

Replace mentions of NEXT_PUBLIC with SANITY_STUDIO in all of the files. Try doing cmd+shift+f in VS code and Find and replace it. It should fix this issue.

I was getting this error Missing environment variable: NEXT_PUBLIC_SANITY_DATASET from the remote studio (sanity.io/manage > Open Sanity Studio). The issue was a mismatch in my local projectroot/sanity/.env.local pared to projectroot/sanity/env.ts.

If you try to hardcode your projectId or dataset but your env.ts is still asserting the .env variables are there you will get this error. Once your env.ts matches your .env.local, run sanity deploy to fix the error.

So in my case, there was a new folder created in the project root. The folder name was "studio-****". It was created when I initialised a new studio with sanity in my project.

Inside it was a new .env.local file.

It had the environment variables with quotation marks/string delimiters around the variables: NEXT_PUBLIC_SANITY_PROJECT_ID="********" NEXT_PUBLIC_SANITY_DATASET="production"

I copied these variables to the .env.local file in the root folder and my app worked immediately.

Just leave your env.ts in the sanity file like this

export const apiVersion =
  process.env.NEXT_PUBLIC_SANITY_API_VERSION || '2024-09-30'


export const dataset = assertValue(
    process.env.NEXT_PUBLIC_SANITY_STUDIO_DATASET,
    'Missing environment variable: NEXT_PUBLIC_SANITY_STUDIO_DATASET'
  )
  
export const projectId = assertValue(
    process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
    'Missing environment variable: NEXT_PUBLIC_SANITY_PROJECT_ID'
  )
  

function assertValue<T>(v: T | undefined, errorMessage: string): T {
  if (v === undefined) {
    throw new Error(errorMessage)
   

  }

  return v
}

You could have another studio folder called "studio—*****" in your root project folder. Inside it is a new .env.local file with environment variables, as shown below. Copy its contents to the main .env.local in your project's root folder.

NEXT_PUBLIC_SANITY_STUDIO_DATASET="production"
NEXT_PUBLIC_SANITY_PROJECT_ID="**********"

After those changes, you will not get that error again

本文标签: javascriptHow do I fix a 39Missing enviornment variable39 in SanityStack Overflow