In this paper, the magnetic field effects on natural convection of power-law nanofluids in rectangular enclosures are investigated numerically with the lattice Boltzmann method. The fluid in the cavity is a water-based nanofluid containing Cu nanoparticles and the investigations are carried out for different governing parameters including Hartmann number (0.0≤Ha≤20.0), Rayleigh number (104≤Ra≤106), power-law index (0.5≤n≤1.0), nanopartical volume fraction (0.0≤ϕ≤0.1) and aspect ratio (0.125≤AR≤8.0). The results reveal that the flow oscillations can be suppressed effectively by imposing an external magnetic field and the augmentation of Hartmann number and power-law index generally decreases the heat transfer rate. Additionally, it is observed that the average Nusselt number is increased with the increase of Rayleigh number and nanoparticle volume fraction. Moreover, the present results also indicate that there is a critical value for aspect ratio at which the impact on heat transfer is the most pronounced.