1

So I'm trying to swap between API links in my angular app based on the origin, but it is an SSR app so I'm trying to account for an environment variable as well as window location, the code is as follows:

const getApiUrl = (): string => {
  if (process && process.env?.AZURE_ENV === 'development') {
    return 'devlink for SSR';
  } else if (
    window &&
    window.location.origin === 'devclient'
  ) {
    return 'devlink for frontendclient';
  } else {
    return 'link.com/';
  }
};

Now the error being thrown out is:

Uncaught ReferenceError: process is not defined

I've digged into the 'compiled' script and have 100% confirmed it's coming from this piece of code.

Shouldn't this still work though?

I've also tried a vesion where I just have if(process) and get the exact same result as above.

SebastianG
  • 8,563
  • 8
  • 47
  • 111
  • 2
    Does this answer your question? [JavaScript check if variable exists (is defined/initialized)](https://stackoverflow.com/questions/5113374/javascript-check-if-variable-exists-is-defined-initialized) – Klaycon Dec 19 '19 at 22:22

4 Answers4

2

Probably it is not there so it will fail to evaluate, maybe testing it like typeof process !== 'undefined' will help

If process has never been defined in any accessible context I think it will fail with an UncaughtReferenceError because it is trying to access something not in the context. Undefined means that the variable exists but it has no value set and this error says that the variable is just not there, thats why checkinng the type of it will probably solve the issue.

A. Llorente
  • 1,142
  • 6
  • 16
  • but shouldn't process then be falsey as it is undefined? seems like a weird behaviour; it has generally not been an issue trying to evaluate something that's not defined in an if block, that's the weird part that gets me. I'll give the 'typeof' avenue a try right now. – SebastianG Dec 19 '19 at 21:59
  • If process has never been defined in any accessible context I think it will fail with an UncaughtReferenceError because it is trying to access something not in the context. Undefined means that the variable exists but it has no value set and this error says that the variable is just not there, thats why checkinng the type of it will probably solve the issue. I cannot test it now as I am in the phone but give it a try – A. Llorente Dec 19 '19 at 22:06
  • This indeed works and learnt something new about JS, thanks! – SebastianG Dec 20 '19 at 13:35
1

Nope. While a non-exising field of an object is really undefined, read access to a non-existing variable is an error in JavaScript, use typeof as other answers suggest...

console.log("typeof {}.process",typeof {}.process);
console.log("typeof process",typeof process);
console.log("{}.process",{}.process);
console.log("process",process);

... also, your code is TypeScript, the :string part gives it away. Which means it is compiled to strict mode, and even write access to a non-existing non-local variable would be an error.

tevemadar
  • 12,389
  • 3
  • 21
  • 49
0

I'm not very familiar with angular. But typically this is supposed to be client side code and it doesn't have access to node environment variables. Even if it is SSR.

If you are using webpack you could define the environment variable as a global variable on the client side:

https://webpack.js.org/plugins/define-plugin/

T. Short
  • 3,481
  • 14
  • 30
  • That's not an issue as I don't need it/expect it to be there. The issue I'm having is not being to evaluate if (undefined variable) {} – SebastianG Dec 19 '19 at 22:00
0

For your case if you don't want to use any other solutions, firstly check global , its like window but on node, so process will be stored in global.process

VitoMakarevich
  • 439
  • 2
  • 5