are investing an enormous/absurd amount of production
Don't overestimate constructor cost. Since you can drop engines and life support when you are building economic starbases, you pay about 50-60 manufacturing per module.
Lets add up the cost:
Economic SB + factory module + zero G scaling + Orbital Replicator + Interstellar collector
= 5 consturction modules (250-300 manufacturing) for +5% raw production and +50% mnufacturing
Compare this to a planetary improvement:
Basic Factory (30 manufacturing) + Xeno Factory (45 manufacturing) + Mega Factory (67 manufacturing) + Manufacturing Center (101 manufacturing) + Industrial Sector (151 manufacturing)
= 243 manufacturing for 40% manufacturing or 394 manufacturing for 50% manufacturing.
If your starbase affects multiple planets this is not a decision any more. And the hidden benefit of it all is that starbases enable you to have more population on your planet because they cost 0 tiles and eventually bring some approval on the table, too. Plus the push the optimal blance of on planet M/R/W-modifiers vs. food bonusses in favor of the food bonusses. And population is multiplicative with both +% raw production and +% manufacturing.
At some point you will be building starbases from the net profit of the others you have built.
why aren't they just militarily conquered by that opponent sitting next to them and investing into military instead?
Most simple explanation?
Difficulty. I usually play on Genius and my military buildup is usually just in time. I keep my nearest neighbors appeased with trade routes primarily. If necessary I get a little bit of diplomacy tech promise friendship. So the first other civ to declare war to me often has to pass through one of my neighbors territories first.
Also I am not building them at once. In more practical cases I will have like 5 or 9 SBs per system (don't forget SBs can benefit multiple planets) and by the point where I could scale up to 14 per planet (if no asteroids, nebulas, planets etc. are in the way...) - which is age of ascension tech - the game is way past decided anyways.
Note that 18 SB requires the vigilant trait. There are other traits I like better.