A search is presented for a right-handed W boson (W-R) and a heavy neutrino (N), in a final state consisting of two same-flavor leptons (ee or mu mu) and two quarks. The search is performed with the CMS experiment at the CERN LHC using a data sample of proton-proton collisions at a center-of-mass energy of 13TeV corresponding to an integrated luminosity of 138 fb(-1). The search covers two regions of phase space, one where the decay products of the heavy neutrino are merged into a single large-area jet, and one where the decay products are well separated. The expected signal is characterized by an excess in the invariant mass distribution of the final-state objects. No significant excess over the standard model background expectations is observed. The observations are interpreted as upper limits on the product of W-R production cross sections and branching fractions assuming that couplings are identical to those of the standard model W boson. For N masses m(N) equal to half the W-R mass m(WR) (m(N) = 0.2TeV), mW(R) is excluded at 95% confidence level up to 4.7 (4.8) and 5.0 (5.4) TeV for the electron and muon channels, respectively. This analysis provides the most stringent limits on the WR mass to date.