How do I make my service dependent on another module existing? I want to provide a service in my module, but only if another module (say the core File module) is enabled. I don't want to require this module, it should be optional.

Something like this:

services:
  helper.file:
    class: Drupal\helper\File
    arguments: ['@entity_type.manager']
    requirements: ['file']


Is there a way to do this without needing to declare it as a dynamic service? If not, is there an example on how to declare a dynamic service, because https://www.drupal.org/node/2026959 is woefully lacking:


  Finally, it is also possible to define the register() method to register services dynamically, however this should be very rare.
Yep, you need a ServiceProvider that only registers the service if the module exists in the container.modules parameter

See https://github.com/md-systems/file_entity/blob/8.x-2.x/src/FileEntityServiceProvider.php#L25 for an example