The phrase such as requires a comma in front of it only if it’s part of a nonrestrictive clause.
When to Use a Comma Before Such As
Here’s an example of such as used correctly with a comma in a sentence:
The phrase such as pine and spruce is nonrestrictive, so you need a comma. How can you tell it’s nonrestrictive? Take the phrase out and see whether the sentence is still true: In this forest, you’ll see many types of coniferous trees.
When to Use Such As Without a Comma
Don’t use a comma if such as is part of a restrictive clause.
Here we have a restrictive phrase. Try taking it out of the sentence: Trees don’t grow at this altitude. The sentence isn’t true anymore. We’re not trying to say that no trees grow at this altitude—we’re restricting the statement to specific types of trees: oaks and elms.
How to Use Such As in a Sentence
Use such as to provide specific examples of something you’re talking about. If the specific examples aren’t essential to the accuracy of your sentence, then use a comma before such as and after your example, unless the example is at the very end of the sentence.
If the examples are essential to the meaning of your sentence, omit the commas.