Bandgap engineering is used to create semiconductor heterostructure devices that perform processes such as resonant tunnelling and solar energy conversion. However, the performance of such devices degrades as their size is reduced. Graphene-based molecular electronics has emerged as a candidate to enable high performance down to the single-molecule scale. Graphene nanoribbons, for example, can have widths of less than 2 nm and bandgaps that are tunable via their width and symmetry. It has been predicted that bandgap engineering within a single graphene nanoribbon may be achieved by varying the width of covalently bonded segments within the nanoribbon. Here, we demonstrate the bottom-up synthesis of such width-modulated armchair graphene nanoribbon heterostructures, obtained by fusing segments made from two different molecular building blocks. We study these heterojunctions at subnanometre length scales with scanning tunnelling microscopy and spectroscopy, and identify their spatially modulated electronic structure, demonstrating molecular-scale bandgap engineering, including type I heterojunction behaviour. First-principles calculations support these findings and provide insight into the microscopic electronic structure of bandgap-engineered graphene nanoribbon heterojunctions.