Measuring the metallicity and carbon-to-oxygen (C/O) ratio in exoplanet atmospheres is a fundamental step towards constraining the dominant chemical processes at work and, if in equilibrium, revealing planet formation histories. Transmission spectroscopy (for example, refs. (1,2)) provides the necessary means by constraining the abundances of oxygen- and carbon-bearing species; however, this requires broad wavelength coverage, moderate spectral resolution and high precision, which, together, are not achievable with previous observatories. Now that JWST has commenced science operations, we are able to observe exoplanets at previously uncharted wavelengths and spectral resolutions. Here we report time-series observations of the transiting exoplanet WASP-39b using JWST's Near InfraRed Camera (NIRCam). The long-wavelength spectroscopic and short-wavelength photometric light curves span 2.0-4.0 micrometres, exhibit minimal systematics and reveal well defined molecular absorption features in the planet's spectrum. Specifically, we detect gaseous water in the atmosphere and place an upper limit on the abundance of methane. The otherwise prominent carbon dioxide feature at 2.8 micrometres is largely masked by water. The best-fit chemical equilibrium models favour an atmospheric metallicity of 1-100-times solar (that is, an enrichment of elements heavier than helium relative to the Sun) and a substellar C/O ratio. The inferred high metallicity and low C/O ratio may indicate significant accretion of solid materials during planet formation (for example, refs. (3,4)(,)) or disequilibrium processes in the upper atmosphere (for example, refs. (5,6)).