Reputation: 51
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
Upvotes: 5
Views: 5426
Reputation: 1
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
Upvotes: 0
Reputation: 93
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.
Upvotes: 0
Reputation: 31
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
compared 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.
Upvotes: 0
Reputation: 1
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.
Upvotes: 0
Reputation: 341
You must use the SANITY_STUDIO_
prefix, not the NEXT_PUBLIC_
prefix.
See Sanity.io documentation on env variables
To fix your error:
Replace mentions of NEXT_PUBLIC_SANITY_DATASET
with SANITY_STUDIO_DATASET
Ensure SANITY_STUDIO_DATASET
is set with correct project ID in your environment
Upvotes: 5
Reputation: 21
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
Upvotes: 2