Here's the general rule:
* Use a comma if "such as" introduces a list of examples that follows a complete thought.
Examples:
* Correct: I like many fruits, such as apples, oranges, and bananas. ("I like many fruits" is a complete thought, and "such as" introduces the list.)
* Incorrect: I like many fruits such as apples, oranges, and bananas. (The sentence lacks a comma, making it unclear where the list begins.)
Use no comma if "such as" introduces a short, essential phrase that is not a list.
Examples:
* Correct: I like fruits such as apples. (The phrase "such as apples" is necessary to clarify what kind of fruits the speaker likes.)
* Incorrect: I like fruits, such as apples. (The comma creates an unnecessary pause and misrepresents the intended meaning.)
In essence, it's about the structure and meaning of your sentence. Ask yourself: does the phrase after "such as" provide additional information or examples that are not strictly necessary to understand the main idea? If yes, use a comma. If not, you don't need one.