Chemical nociception, the detection of tissue-damaging chemicals, is important for animal survival and causes human pain and inflammation, but its evolutionary origins are largely unknown. Reactive electrophiles are a class of noxious compounds humans find pungent and irritating, such as allyl isothiocyanate (in wasabi) and acrolein (in cigarette smoke). Diverse animals, from insects to humans, find reactive electrophiles aversive, but whether this reflects conservation of an ancient sensory modality has been unclear. Here we identify the molecular basis of reactive electrophile detection in flies. We demonstrate that Drosophila TRPA1 (Transient receptor potential A1), the Drosophila melanogaster orthologue of the human irritant sensor, acts in gustatory chemosensors to inhibit reactive electrophile ingestion. We show that fly and mosquito TRPA1 orthologues are molecular sensors of electrophiles, using a mechanism conserved with vertebrate TRPA1s. Phylogenetic analyses indicate that invertebrate and vertebrate TRPA1s share a common ancestor that possessed critical characteristics required for electrophile detection. These findings support emergence of TRPA1-based electrophile detection in a common bilaterian ancestor, with widespread conservation throughout vertebrate and invertebrate evolution. Such conservation contrasts with the evolutionary divergence of canonical olfactory and gustatory receptors and may relate to electrophile toxicity. We propose that human pain perception relies on an ancient chemical sensor conserved across approximately 500 million years of animal evolution.