OP, road racer here,
Since the majority of us use some sort of instrumentation on our bikes for training purposes (power meters of some sort, or at least heart rate monitoring) and we do periodic testing to know among other things what our anaerobic threshold power/heart rate is, we also know how hard we can go on a long sustained climb without blowing up after only a few minutes. Riding up mountains is still work for us, but we don't have to stop partway up either because we use that information to pace ourselves.
You're 'just riding' so you don't likely have any of that technological help, but there's still advice I can give you that will help.
Many average riders think they need to go as hard as they can when climbing, especially if they're with a group and don't want to fall behind. That works fine if it's a very short hill that only takes a minute or two at most to climb, you'll be breathing hard at the top, but more than a few minutes like that and you blow up completely partway up like you're experiencing.
The key to climbing is "rider, know thyself". You have to know what your limits are and respect them. This is not saying 'just go slow, bro', never pushing your limits (which is how you get better at climbing), but you have to have some sense of where your anaerobic threshold is, and stay *just below it* in what we call the 'sweet spot'. It's hard work climbing at that intensity, but doing it regularly and repeatedly is what makes you a better climber over the long run.
Also it's okay to slow down on a climb if you think you're going to blow up.
The best strategy here is to start slow, and build up intensity in steps over several minutes. If you can go, say, 5 minutes at an intensity and feel like you're just on the brink of blowing up, it's hard but sustainable, then you're probably right near your anaerobic threshold.
Come back in a month or so and tell us how you're doing.