A bstract The second-order ( v 2 ) and third-order ( v 3 ) Fourier coefficients describing the azimuthal anisotropy of prompt and nonprompt (from b-hadron decays) J / ψ, as well as prompt ψ(2S) mesons are measured in lead-lead collisions at a center-of-mass energy per nucleon pair of $$ \sqrt{s_{\textrm{NN}}} $$ <mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"><mml:msqrt><mml:msub><mml:mi>s</mml:mi><mml:mi>NN</mml:mi></mml:msub></mml:msqrt></mml:math> = 5 . 02 TeV. The analysis uses a data set corresponding to an integrated luminosity of 1.61 nb − 1 recorded with the CMS detector. The J / ψ and ψ(2S) mesons are reconstructed using their dimuon decay channel. The v 2 and v 3 coefficients are extracted using the scalar product method and studied as functions of meson transverse momentum and collision centrality. The measured v 2 values for prompt J / ψ mesons are found to be larger than those for nonprompt J / ψ mesons. The prompt J / ψ v 2 values at high p T are found to be underpredicted by a model incorporating only parton energy loss effects in a quark-gluon plasma medium. Prompt and nonprompt J / ψ meson v 3 and prompt ψ(2S) v 2 and v 3 values are also reported for the first time, providing new information about heavy quark interactions in the hot and dense medium created in heavy ion collisions.