The production of the W± bosons measured in p–Pb collisions at a centre-of-mass energy per nucleon–nucleon collision sqrt{s_{textrm{NN}}} = 8.16 TeV and Pb–Pb collisions at sqrt{s_{textrm{NN}}} = 5.02 TeV with ALICE at the LHC is presented. The W± bosons are measured via their muonic decay channel, with the muon reconstructed in the pseudorapidity region −4 < {eta}_{textrm{lab}}^{mu } < −2.5 with transverse momentum {p}_{textrm{T}}^{mu } > 10 GeV/c. While in Pb–Pb collisions the measurements are performed in the forward (2.5 < {y}_{textrm{cms}}^{mu } < 4) rapidity region, in p–Pb collisions, where the centre-of-mass frame is boosted with respect to the laboratory frame, the measurements are performed in the backward (−4.46 < {y}_{textrm{cms}}^{mu } < −2.96) and forward (2.03 < {y}_{textrm{cms}}^{mu } < 3.53) rapidity regions. The W− and W+ production cross sections, lepton-charge asymmetry, and nuclear modification factors are evaluated as a function of the muon rapidity. In order to study the production as a function of the p–Pb collision centrality, the production cross sections of the W− and W+ bosons are combined and normalised to the average number of binary nucleon–nucleon collision 〈Ncoll〉. In Pb–Pb collisions, the same measurements are presented as a function of the collision centrality. Study of the binary scaling of the W±-boson cross sections in p–Pb and Pb–Pb collisions is also reported. The results are compared with perturbative QCD calculations, with and without nuclear modifications of the Parton Distribution Functions (PDFs), as well as with available data at the LHC. Significant deviations from the theory expectations are found in the two collision systems, indicating that the measurements can provide additional constraints for the determination of nuclear PDFs and in particular of the light-quark distributions.