A search for a heavy right-handed W boson (WR) decaying to a heavy right-handed neutrino and a charged lepton in events with two same-flavor leptons (e or μ) and two jets, is presented. The analysis is based on proton-proton collision data, collected by the CMS Collaboration at the LHC in 2016 and corresponding to an integrated luminosity of 35.9 fb−1. No significant excess above the standard model expectation is seen in the invariant mass distribution of the dilepton plus dijet system. Assuming that couplings are identical to those of the standard model, and that only one heavy neutrino flavor NR contributes significantly to the WR decay width, the region in the two-dimensional (mWRmNR) mass plane excluded at 95% confidence level extends to approximately mWR=4.4 TeV and covers a large range of right-handed neutrino masses below the WR boson mass. This analysis provides the most stringent limits on the WR mass to date.[Figure not available: see fulltext.].