This article is almost certainly the answer to your question:
If not, could you tell us more about how you are trying to use that value? That javascript looks fine, assuming that:
you have that variable set during build
that code RUNS during build (not, say, at browse time - where that is just a string, or anyway the environment variable isn’t set anymore) and interpolates its output into your static files.