1

I want to set up a "constant" variable in my angular2 application. But, depending if a system variable is set or not.

Now I have this:

export const backendUrl: string = process.env.BACKEND_URL || 'http://localhost:8080';

Basically, I want that, if the System variable BACKEND_URL is set, the constant backendUrl is set to that system variable, but, if not, then it is localhost.

I want that to make the backend url different depending if I am in production or development.

But I am getting the error that the process variable is not defined. How can I do that in typescript? Or, do you have any better idea on how to change the backend url depending if I am in production or development?

Manuelarte
  • 1,658
  • 2
  • 28
  • 47
  • Angular code executes in the client browser, not in the server in NodeJS. You can't access environment variables defined on your server from code running in the browser, on a different machine. But you can build a different angular2 app for each environment. angular-cli allows doing that. – JB Nizet Nov 12 '16 at 13:38
  • But, is there any way then to set a constant from outside? I mean, how can I set up the const backendUrl to be different depending on if I am in production or development? – Manuelarte Nov 12 '16 at 13:39
  • As I just said, do it at build time. – JB Nizet Nov 12 '16 at 13:40
  • So, If I understood properly, I would have like an npm buildDev and a npm buildProd. But then, how can I differenciate the "constant" file that has to be used? – Manuelarte Nov 12 '16 at 13:43
  • 1
    Well, based on whether you passed buildDev or buildProd to npm, for example. Your build process needs to take some kind of input to decide what to include in the build. angular-cli does that automatically for you. – JB Nizet Nov 12 '16 at 13:45

0 Answers0