Discussion on using environment variables in Sanity Studio for different environments

11 replies
Last updated: May 28, 2020
Apologize for asking the same question again, but this is a bit of a roadblock for me at the moment...I'm trying to render my desk structure differently depending on whether Sanity Studio is being run via
sanity start
or has been built with
sanity build
.I've tried using environment variables, but it doesn't seem like any of the environment variables get passed through to the front end even if I prepend them with
SANITY_STUDIO_
.If anyone can help, it would be greatly appreciated.
May 27, 2020, 8:05 PM
Have you created the files .
env.development
&
.env.production
?
May 27, 2020, 8:11 PM
or how do you create the variables?
May 27, 2020, 8:11 PM
Indeed, what Hafffe said - you would need these two specific file names (not just
.env
) to have the vars get picked up at
sanity start
(
.env.development
) or `sanity build`/`sanity deploy` (
.env.production
). The prefix you’re using looks correct!
May 28, 2020, 11:16 AM
Indeed, what Hafffe said - you would need these two specific file names (not just
.env
) to have the vars get picked up at
sanity start
(
.env.development
) or `sanity build`/`sanity deploy` (
.env.production
). The prefix you’re using looks correct!
May 28, 2020, 11:16 AM
Okay, I will give that a shot.So does that mean
this bit of documentation isn't accurate?

The environment is defined by the value of the 
SANITY_ACTIVE_ENV
 environment variable. If not defined, it will default to 
development
 when running the 
sanity start
 command, while it will use 
production
 when running 
sanity build
 and 
sanity deploy
.
You're saying I'll actually need to create a
.env.production
with something like
SANITY_STUDIO_ENVIRONMENT="production"
and a
.env.development
containing
SANITY_STUDIO_ENVIRONMENT="development"
...? There isn't an automatic way to determine within code if you're in dev or prod?
May 28, 2020, 2:44 PM
Ah, no, both
SANITY_ACTIVE_ENV
and
NODE_ENV
should be available without needing those
.env.*
files. How are you checking for them? Normally you’d use something like this:
const env = process.env.SANITY_ACTIVE_ENV || process.env.NODE_ENV || 'development'
(To test, try without the latter
|| 'development'
part)
May 28, 2020, 3:13 PM
Yeah, that is exactly what I'm currently doing. It's returning
undefined
.
May 28, 2020, 3:56 PM
And I'm doing that in a desk structure implementation file, to be clear.
May 28, 2020, 3:57 PM
Hmm that’s strange indeed - just tried to reproduce. When I add this to the top of `deskStructure.js`:
const env = process.env.SANITY_ACTIVE_ENV || process.env.NODE_ENV
console.log('env:',env)
I get
env: development
logged to console in a development environment (
sanity start
).
Are you getting
undefined
for the exact same approach?
May 28, 2020, 4:00 PM
Lemme try one tweak...
May 28, 2020, 4:04 PM
Okay...Sorry about that – got caught up in some other things.Looks like
NODE_ENV
does work. It's
SANITY_ACTIVE_ENV
that is
undefined
. So that gives me something usable. 👍
May 28, 2020, 5:27 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?