Atomic comagnetometers are used in searches for anomalous spin-dependent interactions. Magnetic field gradients are one of the major sources of systematic errors in such experiments. Here we describe a comagnetometer based on the nuclear spins within an ensemble of identical molecules. The dependence of the measured spin-precession frequency ratio on the first-order magnetic field gradient is suppressed by over an order of magnitude compared to a comagnetometer based on overlapping ensembles of different molecules. Our single-species comagnetometer is capable of measuring the hypothetical spin-dependent gravitational energy of nuclei at the 10−17 eV level, comparable to the most stringent existing constraints. Combined with techniques for enhancing the signal such as parahydrogen-induced polarization, this method of comagnetometry offers the potential to improve constraints on spin-gravity coupling of nucleons by several orders of magnitude.