I have created a drush service and I can use it on the command line, but I am trying to use dependency injection to use it from another class. However, it is saying that the service cannot be found. (From the public function __construct) All other regular services can be injected and used, except for the drush service. Can I inject drush services? Is injecting them different from other injection dependencies?

I have add the changes in the composer,json {extra}, created drush.services.yml and the Commands.php file. Everything works except the dependency injection.

I'm using Drupal 8, drush 9


The NlmCommands.php file code

class NlmCommands extends DrushCommands {
//drush test service
public function hello($name = , $options = ['msg' => FALSE]) {

if ($options['msg']) {
  $this->output()->writeln('Hello ' . $name . '! This is your first Drush 9 command.');
else {
  $this->output()->writeln('Hello ' . $name . '!');


Inject in TestInjection.php

* inject service to form
* @var NlmCommands $hello
protected $migration;

public function __construct(NlmCommands $hello) {
  $monthYear = '0118';
  $this->migration = $hello;

Error is 'call to non-existent service'

  • 1
    Can you post your code?
    – Kevin
    Jan 18 '19 at 1:56
  • I believe Drush services are not the same as Drupal services, so you will need to create a [module].services.yml file and register your service there.
    – Jaypan
    Jan 18 '19 at 6:07
  • I have different types of services in the module, and have the module.services.yml file and drush.services.yml file.
    – user38877
    Jan 18 '19 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.