If you are building an app you should not assume that theme.js even exists. Themes are not required to have a file named like that in their Assets folder. In fact, themes aren't even required to have jQuery. Even if you did check that theme.js exists, I would not recommend "injecting" anything in there - when a theme is updated, your app's changes would all be lost and the result would be a broken theme / storefront.
Think of your app as an entirely self-contained unit. Do not make any assumptions about a theme beyond those required by Shopify (i.e. certain folder structures etc.) and really, for the benefit of everyone, try not to modify existing templates or assets if avoidable.
A theme has its own script features which can be in any JS file in the Assets folder and named whatever the theme designer prefers. The most common name seems to be theme.js simply because that's what is most often used by Shopify's free themes. The important part to consider is that these are theme features NOT app features or extensions. Of course, many people may customise their theme's scripts by editing and adding to the theme's default scripting files, but that would make future updates quite difficult. An app should not engage in the same practice, especially because an app's feature is not a theme feature.
Instead, you should be serving your own JS file. If your app relies on jQuery, you can check whether another jQuery already exists as this is very likely and you wouldn't need to load yet another one. If it doesn't you can dynamically load jQuery.
|2 hours ago|
|2 hours ago|