It all starts with the correct sitting position. If it is adjusted correctly, the pain usually disappears.
First adjust the seat height. The following applies: crotch length x 0.893 equals seat height from the centre of the bottom bracket to the top of the saddle. The back must not be too arched. If the saddle is too high, a steeper stem is necessary. Now adjust the saddle position. When seated, the plumb line should fall from the tibia head (under the kneecap) through the pedal axle - the knee joint is exactly above the pedal axle.
The ergonomics expert checks this in the shop: Two flat laser devices, which are aligned from the front to the centre of the pedal/knee axis, can be used to determine whether the knees are in the correct position. And: Are your arms and wrists in line? If not, change handlebars.