Issue with environment variables not working in the sanity.config.js file when deploying to Sanity studio.

11 replies
Last updated: Jan 4, 2023
I want to report a bug.
If you want to use the process.env
SANITY_STUDIO_API_PROJECT_ID
and
SANITY_STUDIO_API_DATASET
it will not work inside the
*<http://sanity.cl|sanity.cl>*i.ts
file if you try to deploy the sanity studio it will return the env variables as
undefined
to solve this you have to add a prefix with
VITE_
😄 This only happens in production and deployment though.
So the current fix for the guys out there having the same issues instead of doing this:


import { defineCliConfig } from 'sanity/cli'

const projectId = process.env.SANITY_STUDIO_API_PROJECT_ID
const dataset = process.env.SANITY_STUDIO_API_DATASET

export default defineCliConfig({ api: { projectId, dataset } })
Do this:

import * as dotenv from 'dotenv'

dotenv.config()

import { defineCliConfig } from 'sanity/cli'

const projectId = process.env.SANITY_STUDIO_API_PROJECT_ID
const dataset = process.env.SANITY_STUDIO_API_DATASET

export default defineCliConfig({ api: { projectId, dataset } })
Dec 20, 2022, 9:34 PM
That config file is for the CLI, which does not run in the browser. If you're using environment variables there you will need to use
dotenv
. I don't believe this is a bug, just a side effect of how env vars work.
Dec 20, 2022, 10:21 PM
Hey hey 🙂 Did you follow these steps ?
Dec 21, 2022, 10:43 AM
user Q
Thank you so much for this fix, I was halfway there with the help from
user M
but i hadn't converted from "import.meta.env" to "process.env"

user J
the steps on that page work fine for local deployment but seem to break trying to deploy to sanity or deploy the graphql schema
Dec 21, 2022, 3:31 PM
I will investigate further with
user M
…
Dec 21, 2022, 4:28 PM
This is what I had to do to get it to work both locally and when deploying. I'm sure there's a much better way to do this but it's a bandaid fix for now.
Dec 21, 2022, 4:30 PM
Hi
user E
I am going through my old threads and was wondering if you still have this issue?I can see you are mixing imports still

if (!projectId || !dataset || !googleMapsApiKey) {
  dotenv.config()
  projectId = process.env?.SANITY_STUDIO_PROJECT_ID
  dataset = process.env?.SANITY_STUDIO_DATASET
  googleMapsApiKey = process.env?.SANITY_STUDIO_GOOGLE_MAPS_API_KEY
}
You do it the right way in the beginning and then change back to process.env.
Jan 2, 2023, 4:56 PM
Running locally require import.meta.env but when trying to run sanity deploy it returns null, but process.env seems to work. So I check if import.meta.env returned anything and if not I use the dotenv method
Jan 2, 2023, 5:14 PM
Well, tbh, i dont even understand what you’re issue is now. I think you talked to
user M
inanother thread.Can you please share what issue you’re experiencing?
Jan 2, 2023, 5:56 PM
I'll try to explain what's happening as best I can, although I'm not incredibly well versed in env variables and how these things work under the hood so please forgive me if I dont explain well or am missing something.
This is my sanity.config.js file. First try to load the env variables as instructed the sanity v3 documentation. This works when I run
sanity dev
and
sanity deploy
. This however does not seem to work when I try to run
sanity graphql deploy
which fails with this error `Error: Configuration must contain `projectId`` . To combat this I check to see if the values for projectId, dataset, and googleMapsApiKey exist, and if not i use the dotenv method to retrieve these which was recommended by
user M
.
This seems to fix the issue. The reason I dont just use the dotenv method only is because it seems to ONLY work when trying to run
sanity graphql deploy
. So first I try to get the env variables with the recommended method, then use dotenv if that fails
Jan 3, 2023, 8:02 PM
Thank you for the explanation 🙂 Are you able to deploy with the workaround now?
Jan 4, 2023, 10:01 AM
Yes the workaround seems to be functioning
Jan 4, 2023, 2:28 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?