Elevational diversity gradients describe how species diversity and richness change with elevation. In this study, we tested for the effect of climate, resource quality and habitat heterogeneity on earthworm communities along an elevational gradient and around the treeline in the French Alps. Earthworm communities and environmental properties (i.e. climate, soil properties, vegetation structure and composition) were sampled at six elevations ranging from 1400 to 2400 m. Results were analysed through multi-table factorial analyses and structural equation modelling. We found average density, biomass and species richness in the range of that usually reported in comparable ecosystems. We found no monotonic decrease in species richness along the elevational gradient, which we explain by the species pool being dominated by taxa with high environmental tolerance and dispersal capacities. Instead, we highlighted the ecotone associated with the treeline as the primary driving factor of earthworm communities: at 1800–2000 m elevation, communities were more abundant and diverse, and had a greater variability in body mass. This result was largely explained by the structure and composition of the vegetation, whereas soil and climate appeared to have only indirect effects. Therefore, the treeline effect on earthworm communities can be explained both by the effect of environmental heterogeneity and of trophic resource quality that increases at the ecotone level.