the bulb filament is probibly broken, tapping makes the broken ends touch breifly making a cuircut.
I would try replacing the bulb. try the bulb from the other headlight if you want to check the wires before spending money on a new bulb.
warning= do not touch bulb with bare fingers.