Metal–organic framework (MOF) membranes have received increasing attention as adsorbents, yet the defects in most membrane structures greatly thwart their capacity performance. In this work, we fabricated a novel ZnO/2-methylimidazole nanocomposite with multiple morphology by electrochemical method. The nanocomposite provided sufficient and strong anchorages for the zeolitic imidazolate frameworks-8 (ZIF-8) membrane. Thus, a crack-free and uniform MOF membrane with high performance was successfully obtained. In this case, 2-methylimidazole was believed to react with ZnO to form uniform ZIF nuclei, which induced and guided the growth of ZIF-8 membrane. The as-prepared ZIF-8 membrane had large surface area and good thermal stability. As expected, it displayed high adsorption capacity for acidic drugs (e.g., ibuprofen, ketoprofen and acetylsalicylic acid) as they could interact through hydrophobic, hydrogen bonding and π-π stacking interaction. Accordingly, by coupling with gas chromatography the ZIF-8 membrane was successfully applied to the real-time dynamic monitoring of ibuprofen in patient’s urine.