Slice onions into strips.
Place a large pan over low heat and add the onions and olive oil.
Let the mixture slowly cook for 40 minutes, stirring occasionally as the onions caramelize.
After 40 minutes, when the onions are soft and browning, add balsamic vinegar and brown sugar.
Cook for 5 more minutes and turn off the heat.
Add spaghetti to a large saucepan of salted boiling water and cook according to package instructions until al dente.
Reserve 1 cup pasta cooking water before draining the spaghetti.
Add spaghetti to the pan of caramelized onions along with the butter, Parmesan, and a few splashes of pasta water. Stir well to form a sauce.
Season well with salt and pepper before serving.