Mycobacterial HflX confers resistance against macrolide antibiotics. However, the exact molecular mechanism is poorly understood. To gain further insights, we determined the cryo-EM structures of M.smegmatis (Msm) HflX-50S subunit and 50S subunit-erythromycin (ERY) complexes at a global resolution of approximately 3Å. A conserved nucleotide A2286 at the gate of nascent peptide exit tunnel (NPET) adopts a swayed conformation in HflX-50S complex and interacts with a loop within the linker helical (LH) domain of MsmHflX that contains an additional 9 residues insertion. Interestingly, the swaying of this nucleotide, which is usually found in the non-swayed conformation, is induced by erythromycin binding. Furthermore, we observed that erythromycin decreases HflX's ribosome-dependent GTP hydrolysis, resulting in its enhanced binding and anti-association activity on the 50S subunit. Our findings reveal how mycobacterial HflX senses the presence of macrolides at the peptide tunnel entrance and confers antibiotic resistance in mycobacteria.