Synthesis of methyl esters using diazomethane:
Every step in the reaction mechanism is in equilibrium and thus it is significant to use the alcohol in large excess (that is as solvent) in order to drive the equilibrium to products. This is just practical with cheap and readily available alcohols like methanol and ethanol. Alternatively, if the carboxylic acid is cheap and readily available it could be employed in large excess instead.
An excellent technique of preparing methyl esters is to treat carboxylic acids along with diazomethane shown in figure. Good yields are acquired because nitrogen is formed since one of the products and because it is lost from the reaction mixture, the reaction is driven to completion.
Figure: Synthesis of methyl esters using diazomethane.
Though, diazomethane is very much hazardous chemical that can explode, and strict precautions are essential while using it.
Finally, the carboxylic acid can be transformed to a carboxylate ion and then treated with an alkyl halide. The reaction includes the SN2 nucleophilic substitution of an alkyl halide and thus the reaction works best with primary alkyl halides.
Figure: Synthesis of an ester by nucleophilic substitution of an alkyl halide.