Understanding complex biological systems requires visualizing structures and processes deep within living organisms. We developed a compact adaptive optics module and incorporated it into two- and three-photon fluorescence microscopes, to measure and correct tissue-induced aberrations. We resolved synaptic structures in deep cortical and subcortical areas of the mouse brain, and demonstrated high-resolution imaging of neuronal structures and somatosensory-evoked calcium responses in the mouse spinal cord at unprecedented depths in vivo.