MOFs‐modified nanofiltration (NF) membranes have been gained a lot of attention due to their favorable permeability and ion selectivity. Nevertheless, the prevailing preparation techniques are afflicted by the incompatibility of MOFs with polymers and the facile loss of MOFs. In this work, polyethyleneimine (PEI) ‐templated ZIF‐8 (PEI‐ZIF‐8) was synthesized and incorporated into the PEI aqueous solution, then interfacial polymerized with trimesoyl chloride (TMC) to obtain the PEI‐ZIF‐8 modified polyamide NF membrane. This PEI modified strategy could endow the ZIF‐8 nanoparticles with positively charged properties to avoid the aggregation and increase the interfacial compatibility with the polyamide. Meanwhile, the appropriate pore size of ZIF‐8 (3.4 Å), which is between the hydration sheath surrounding of Li+ (2 Å) and Mg2+ (4.2 Å) impart the membrane with precise Mg2+/Li+ separation ability. The optimal PEI‐ZIF‐8‐TMC membrane exhibits a permeance of 9 L/h m2bar and a Mg2+/Li+ selectivity of 19, both of which surpass the performance of the pure PEI‐TMC membrane, which has a permeance of 4 L/h m2bar and a Mg2+/Li+ selectivity of 11. Meanwhile, the membrane exhibited excellent long‐term stability of 85h. This novel approach to preparing MOFs‐modified NF membrane represents a promising avenue for the separation of lithium and magnesium.