M dwarf stars, which have masses less than 60 per cent that of the Sun, make up 75 per cent of the population of the stars in the Galaxy. The atmospheres of orbiting Earth-sized planets are observationally accessible via transmission spectroscopy when the planets pass in front of these stars. Statistical results suggest that the nearest transiting Earth-sized planet in the liquid-water, habitable zone of an M dwarf star is probably around 10.5 parsecs away. A temperate planet has been discovered orbiting Proxima Centauri, the closest M dwarf, but it probably does not transit and its true mass is unknown. Seven Earth-sized planets transit the very low-mass star TRAPPIST-1, which is 12 parsecs away, but their masses and, particularly, their densities are poorly constrained. Here we report observations of LHS 1140b, a planet with a radius of 1.4 Earth radii transiting a small, cool star (LHS 1140) 12 parsecs away. We measure the mass of the planet to be 6.6 times that of Earth, consistent with a rocky bulk composition. LHS 1140b receives an insolation of 0.46 times that of Earth, placing it within the liquid-water, habitable zone. With 90 per cent confidence, we place an upper limit on the orbital eccentricity of 0.29. The circular orbit is unlikely to be the result of tides and therefore was probably present at formation. Given its large surface gravity and cool insolation, the planet may have retained its atmosphere despite the greater luminosity (compared to the present-day) of its host star in its youth. Because LHS 1140 is nearby, telescopes currently under construction might be able to search for specific atmospheric gases in the future.