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.

1

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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
This indeed works and learnt something new about JS, thanks!
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.

Comments

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/

1 Comment

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) {}
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.