A method is presented for designing coils for compact stellarators. In contrast to methods that select a finite number of coils from an optimal continuous surface current distribution, the COILOPT code solves for the optimal parameters in an explicit representation of modular coils on a toroidal winding surface that is well separated from the plasma boundary, together with the coefficients of the winding surface. The problem is posed as a balance between approximating a prescribed magnetic configuration and satisfying certain critical engineering requirements. Results are presented for quasi-axisymmetric and quasi-poloidal compact stellarator designs.