We investigate the effect of the magnetic fields of M dwarf (dM) stars on potentially habitable Earth-like planets. These fields can reduce the size of planetary magnetospheres to such an extent that a significant fraction of the planet's atmosphere may be exposed to erosion by the stellar wind. We used a sample of 15 active dM stars, for which surface magnetic-field maps were reconstructed, to determine the magnetic pressure at the planet orbit and hence the largest size of its magnetosphere, which would only be decreased by considering the stellar wind. Our method provides a fast means to assess which planets are most affected by the stellar magnetic field, which can be used as a first study to be followed by more sophisticated models. We show that hypothetical Earth-like planets with similar terrestrial magnetisation (∼1 G) orbiting at the inner (outer) edge of the habitable zone of these stars would present magnetospheres that extend at most up to 6 (11.7) planetary radii. To be able to sustain an Earth-sized magnetosphere, with the exception of only a few cases, the terrestrial planet would either (1) need to orbit significantly farther out than the traditional limits of the habitable zone; or else, (2) if it were orbiting within the habitable zone, it would require at least a magnetic field ranging from a few G to up to a few thousand G. By assuming a magnetospheric size that is more appropriate for the young-Earth (3.4 Gyr ago), the required planetary magnetic fields are one order of magnitude weaker. However, in this case, the polar-cap area of the planet, which is unprotected from transport of particles to/from interplanetary space, is twice as large. At present, we do not know how small the smallest area of the planetary surface is that could be exposed and would still not affect the potential for formation and development of life in a planet. As the star becomes older and, therefore, its rotation rate and magnetic field reduce, the interplanetary magnetic pressure decreases and the magnetosphere of planets probably expands. Using an empirically derived rotation-activity/magnetism relation, we provide an analytical expression for estimating the shortest stellar rotation period for which an Earth-analogue in the habitable zone could sustain an Earth-sized magnetosphere. We find that the required rotation rate of the early-and mid-dM stars (with periods 37-202 days) is slower than the solar one, and even slower for the late-dM stars ( 63-263 days). Planets orbiting in the habitable zone of dM stars that rotate faster than this have smaller magnetospheric sizes than that of the Earth magnetosphere. Because many late-dM stars are fast rotators, conditions for terrestrial planets to harbour Earth-sized magnetospheres are more easily achieved for planets orbiting slowly rotating early-and mid-dM stars.