Instead of replacing the template like you are doing, make your changes to the file at runtime, using the emitter event 'nodebb:ready' like here.

Rename your template to sort-by-votes.tpl so that the original isn't replaced, and only put your changes inside. When the ready event happens, modify the original template by adding your changes to it with a replace(). Like shown here.