1
votes

I have an app service set up in Azure with a static built NodeJS web application. The application runs fine and I want to move the api key and hostname of the api to variables in the environment.

In Azure->Settings->Configuration->Application Settings I have added two environment variables and set a value for each.

In my Constants.js file I have

export const token = process.env.API_KEY;
export const host = process.env.API_HOST;

But process.env.API_KEY is always null.

I have even tried :

export const token = process.env["API_KEY"];

To no avail. What am I doing wrong?

2
Is that code executing server side or in the browser? - evilSnobu
I guess it's browser now I think about it. I haven't created the website I have just done the backend and deployed the website to Azure. - Reptile

2 Answers

0
votes

Both of the code you provided all work on my side.

export const token = process.env.API_KEY;
export const token = process.env["API_KEY"];

Try to redeploy your website and view it in another browser.

enter image description here

Update:

const http = require('http');

//var APPINSIGHTS_INSTRUMENTATIONKEY= process.env.APPINSIGHTS_INSTRUMENTATIONKEY;

const server = http.createServer((request, response) => {
    var a,b,c;
    if(process.env.NODE_ENV === "production")
    {
        a= process.env.NODE_ENV ;
        b= process.env.APPINSIGHTS_INSTRUMENTATIONKEY;
        c= process.env.KEY_VALUE;
    }
    else if(process.env.NODE_ENV === "development")
    {
        a="Welcome";
        b="testparams";
        c="testvalue";
    }
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.end(a+"  && "+b+"myCustomKey="+c);
});

const port = process.env.PORT || 1337;
server.listen(port);

console.log("Server running at http://localhost:%d", port);
0
votes

I ran into this same issue and had the same env vars (mentioned in a follow up answer by the author) showing then finally found that the scripts used by create-react-app specifically expose those vars plus any beginning with REACT_APP_.

I tweaked the logic to allow for additional env vars to pass through, or you could rename you're variables to use the above prefix.