Bring a large pot of heavily salted water to a boil over medium-high heat. Meanwhile, mince 5 garlic cloves. Finely chop 1 medium shallot (about ¼ cup). Trim and grate 2 pounds medium zucchini on the large holes of a box grater into a large bowl (about 7 cups). (Alternatively, fit a food processor with the shredding disc, slice zucchini in half lengthwise, and shred). Finely grate 2 ounces Parmesan cheese (about 1 cup), or measure scant ⅔ cup store-bought grated. Thinly slice 10 fresh basil leaves into ribbons. Juice ½ medium lemon until you have 1 tablespoon.
Melt 1 stick unsalted butter in a 12-inch skillet over medium heat. Add the shallot and garlic and cook until softened, about 1 minute. Add the zucchini, 2 teaspoons kosher salt, and ¼ teaspoon red pepper flakes if using. Cook, stirring occasionally, until the liquid that releases has been cooked off and the zucchini is softened and almost melting, about 15 minutes.
Meanwhile, add 12 ounces dried spaghetti to the boiling water. Cook, stirring every few minutes and separating any noodles that are sticking together, for 1 less minute than al dente according to the package directions. Reserve ¾ cup of the pasta water, then drain the pasta.
When the zucchini is ready, add the pasta, ½ cup of the pasta water, and lemon juice to the skillet. Cook over medium heat, tossing constantly, until the sauce thickens slightly and coats the pasta, 1 to 2 minutes. Toss in more pasta water a tablespoon at a time as needed to reach the desired sauce consistency. Remove from heat, add the Parmesan and basil, and toss to combine. Taste and season with more kosher salt and black pepper as needed. Garnish with more Parmesan cheese if desired.
