There are a few problems:
The usual solution is to put the transformer on the outside of the box. However, it is not clear exactly what you've got - e.g., where is the 120V wiring coming from? Where is the transformer wiring going to?
A typical solution is to use a metal box and mount the transformer on the outside. The low voltage transformer output can then take any path except side-by-side with the 120V wires. The box can also be used as a way to splice to other locations (like your lights), receptacle, switch, whatever.
These instructions for a Hampton Bay transformer illustrate the mounting pretty clearly, but most brands should work pretty much the same way. "Metal box is ugly" you say? Doesn't matter - it is in a closet and it is the safest way to do this. Just make sure to ground the box properly.
Update based on comments
I don't have a before pic. It was a metal blank plate on the junction box with the transformer attached to it.
Aha! That is the right way to install a transformer.
- Pull off plate/transformer (you already did, but starting at step 1)
- Attach new cable (i.e., incoming hot, transformer hot, light hot together; same for neutrals).
- Route new cable out the box to the new location (either through the wall or via wiremold)
- Put the plate/transformer back on, with the transformer (as before, I hope) on the outside.
A typical doorbell transformer generates very little heat. As long as no clothing, boxes, etc. is within a few inches of the transformer, it is safe. The low voltage wiring goes to the doorbell but does not go into or through the junction box.