[Support Guide] Using environment variables on Netlify correctly

Hi all,

I have set my netlify.toml file with [context.deploy-preview.environment] NODE_ENV = "development", but when I create a pull request, my NODE_ENV variable does not change , even if the context indicated during build is ❯ Context deploy-preview

I am using it in a function for a Gatsby site :

function dosmthng() {

  if (process.env.NODE_ENV === "production") {
   // do Something
  } else {    // do Something else }

And the result is always ‘// do something’ , even when I am in deploy-preview context…

What am I missing ?

Hi, @Matos28 :wave:t6: welcome to the forums. Try these things:

  1. Did you make sure that you have saved the changes to your netlify.toml file and committed them to your repository before creating the pull request?
  2. Check that the environment variable NODE_ENV is set correctly; go to the “Build & deploy” section, and click on the "Environment" tab to see a list of environment variables. Make sure that NODE_ENV is listed and set to the correct value for the deploy-preview context.
  3. Finally give this guide a read and see if it helps answer your question: [Support Guide] Compiled Build and Deploy Resources -- start here!

Hello everyone!

I’m wondering is it possible to reassign default Netlify env var to my own env var that is used by my app?
So for instance, Netlify provides $URL env var - url of a deployed site. My app uses $AAA env var.
Is it possible somewhere in the admin dashboard to assign a custom env var this way?

env var AAA = $URL
smth like this:

As stated in this documentation URL is a read-only variable. If you require a different value than provided by Netlify, you’ll need to set another variable @matriar .

@jasiqli thanks,
yeah, I see it is read-only. I don’t want to reassign it, I’d like to use it as a value for another env var.

AFAIK that is not possible. At least not via the UI.

Using this example

possibly you could write that variable to a location then read it.

Or using this one

do preprocessing replace a placeholder value with that of $URL before building.

for anyone who’ll fall here
If you need Netlify env var to be fallthrough as different name:
updates to the build command npm run buildYOUR_VAR=$URL npm run build could make a trick.

It’s a bit ugly and poor scalable but for a MVP it is enough =)

1 Like

Please I need help my site is being attcaked:

please do not open a thread that is not related to your original post.