It is advisable to purchase peanuts still in their shells: you can usually purchase bulk packages at any convenience store.
Pay attention to any external sign of damage: black spots and fungi/molds are obviously not a good sign and the shell should be intact.
You might also want to ensure that your store has many clients or the peanuts you purchase might have been sitting there for too long!
A good way to check for quality peanuts is to shake them: a rattling sound indicates that they’re too dry, while cracked shells and presence of molds indicates that they’re probably already rancid.
Peanut butter should be chosen in light of how natural and unprocessed it is: raw peanut butter, with skin, is much more nutritious (and less likely to be contaminated by aflatoxin), so look for that “natural” label.
Raw peanuts should be stored, with their shell still intact, in a sealed container in a refrigerator: in these conditions they can last up to 3-5 months (fridge) or 6-9 months (freezer) without getting rancid.
Prolonged exposure to humidity and external heat (especially above 40 C) is usually enough to spoil part of the fats, so try not to expose them during those hot and humid summer days!
Precautions for storing peanut butter are even stricter than that, since it doesn’t benefit from the added protection from shells! It is advised to keep your peanut butter in the fridge and try not expose it to light, heat or humidity.