Very often we use definite article before moon, sun, earth, etc but occasionally we don't use. what suggests to use or not to use articles before them?
Welcome to the forum uzma chisti.
We use articles before 'sun' and 'moon' in exactly the same way as we use them before any other countable nouns. We do the same with 'earth' if we regard the word as similar in meaning to 'planet'; if we think of 'Earth' as the name of our planet, then we treat it as a proper noun -without an article in most contexts.