Understanding principles of neurolocomotion requires the synthesis of neural activity, sensory feedback, and biomechanics. The nematode C. elegans is an ideal model organism for studying locomotion in an integrated neuromechanical setting because its neural circuit has a well-characterized modular structure and its undulatory forward swimming gait adapts to the surrounding fluid with a shorter wavelength in higher viscosity environments. This adaptive behavior emerges from the neural modules interacting through a combination of mechanical forces, neuronal coupling, and sensory feedback mechanisms. However, the relative contributions of these coupling modes to gait adaptation are not understood. Here, an integrated neuromechanical model of C. elegans forward locomotion is developed and analyzed. The model consists of repeated neuromechanical modules that are coupled through the mechanics of the body, short-range proprioception, and gap-junctions. The model captures the experimentally observed gait adaptation over a wide range of mechanical parameters, provided that the muscle response to input from the nervous system is faster than the body response to changes in internal and external forces. The modularity of the model allows the use of the theory of weakly coupled oscillators to identify the relative roles of body mechanics, gap-junctional coupling, and proprioceptive coupling in coordinating the undulatory gait. The analysis shows that the wavelength of body undulations is set by the relative strengths of these three coupling forms. In a low-viscosity fluid environment, the competition between gap-junctions and proprioception produces a long wavelength undulation, which is only achieved in the model with sufficiently strong gap-junctional coupling. The experimentally observed decrease in wavelength in response to increasing fluid viscosity is the result of an increase in the relative strength of mechanical coupling, which promotes a short wavelength.