You may be surprised, but the astronauts are also asking this question. This video shows a compass functioning on the ISS https://www.youtube.com/watch?v=5g6y65GrkQE (timelapse). As you can see, a compass will still function well above the Earth's atmosphere. This is because the magnetosphere extends far into space.
And as was mentioned above, there could be another thing - altimeter. For example, altimeter watches help to determine your location as well as a compass.
Anyway, even if compasses are not affected by altitude, they may be subject to other factors that may cause them to malfunction. One of the factors - a very budget model like my fathers' COSTIN (definitely garbage). Just pay a bit more but it will be a quality product.

~Kevin


Edited by packlite (04/25/20 09:26 AM)
Edit Reason: edited out link