The nuclear modification factors of J / ψ and ψ(2S) mesons are measured in PbPb collisions at a centre-of-mass energy per nucleon pair of sNN=5.02TeV. The analysis is based on PbPb and p p data samples collected by CMS at the LHC in 2015, corresponding to integrated luminosities of 464μb-1 and 28pb-1, respectively. The measurements are performed in the dimuon rapidity range of | y| < 2.4 as a function of centrality, rapidity, and transverse momentum (pT) from pT= 3 GeV/c in the most forward region and up to 50GeV/c. Both prompt and nonprompt (coming from b hadron decays) J / ψ mesons are observed to be increasingly suppressed with centrality, with a magnitude similar to the one observed at sNN=2.76TeV for the two J / ψ meson components. No dependence on rapidity is observed for either prompt or nonprompt J / ψ mesons. An indication of a lower prompt J / ψ meson suppression at pT> 25 GeV/c is seen with respect to that observed at intermediate pT. The prompt ψ(2S) meson yield is found to be more suppressed than that of the prompt J / ψ mesons in the entire pT range.