Cooperative interactions constitute the backbone of many biological and social systems. Since cooperation is prone to exploitation, these systems must incorporate mechanisms that prevent the spreading of defective behaviors. One such mechanism is modularity, i.e., the tendency of a social network to be organized in modules, where individuals within a module tend to interact strongly among themselves while avoiding interacting with individuals from other modules. This structure allows cooperation to prevail by having modules of cooperative individuals with a limited exposure to defectors. To address the rate and shape of the effect of modularity on the resilience of cooperation, here we study a variant of the Prisoner’s Dilemma on modular networks. Our simulations reveal a sharp transition between a resilient and a vulnerable regime as modularity exceeds a critical threshold. By using a simplified mathematical model, we show that the observed threshold is equivalent to the epidemic threshold found in a certain class of SIR models. This allows us to derive an explicit condition under which a cooperative society is expected to be resilient to invasive defectors.