Miniaturized spectrometers have significant potential for portable applications such as consumer electronics, health care, and manufacturing. These applications demand low cost and high spectral resolution, and are best enabled by single-shot free-space-coupled spectrometers that also have sufficient spatial resolution. Here, we demonstrate an on-chip spectrometer that can satisfy all of these requirements. Our device uses arrays of photodetectors, each of which has a unique responsivity with rich spectral features. These responsivities are created by complex optical interference in photonic-crystal slabs positioned immediately on top of the photodetector pixels. The spectrometer is completely complementary metal–oxide–semiconductor (CMOS) compatible and can be mass produced at low cost.