Adding a library to a module using composer I'm working on a module which will require a library that is already in composer.

I added a line to my module's composer.json file:

"require": {
   "markrogoyski/math-php" : "dev-master"
}


I ran the command:

composer update .


and a "vendor" directory was created in my module's directory and the files were downloaded.  However, If I add a use statement to the file my_module/src/Controller/Test.php I get an error:

----Test.php----

&lt;?php
namespace Drupal\lims\Controller;

use Drupal\Core\Controller\ControllerBase;
use Math\Functions\Map\Single;

class Sample extends ControllerBase {
    public function content() {
        print_r(Single::square([1,2,3,4,5]));
    }
}


I see the error:

PHP Fatal error:  Class 'Math\\Functions\\Map\\Single' not found in [module path]/src/Controller/Test.php on line 9


Is there another step that I need to do to get the library autoloaded in the correct namespace?
Using composer update, Composer created the vendor directory, and the autoload.php file, which is necessary to autoload the library. Unfortunately, Drupal doesn't load all the autoload.php files found in each of the directories containing a module.

Instead of adding the dependency of your module in the composer.json file used by Drupal, you should edit its extra/merge-plugin section to include the directory containing the modules you add.  (I added the "modules/*/composer.json" line.)

"extra": {
    "_readme": [
        "By default Drupal loads the autoloader from ./vendor/autoload.php.",
        "To change the autoloader you can edit ./autoload.php."
    ],
    "merge-plugin": {
        "include": [
            "core/composer.json",
            "modules/*/composer.json"
        ],
        "recurse": false,
        "replace": false,
        "merge-extra": false
    }
},


In this way, running composer update from the Drupal root directory would  add the required classes in the vendor directory used by Drupal, making them automatically loaded.  

If the modules you install are all hosted on drupal.org (i.e., you don't use any custom module), you can instead edit the repositories section to contain the following lines.

"repositories": {
    "drupal": {
        "type": "composer",
        "url": "https://packagist.drupal-composer.org"
    }
}


In this way, you could add a module hosted on drupal.org running composer require drupal/module-name from the Drupal root directory, and Composer would install its dependencies (declared in the module's composer.json file) in the vendor directory, making the classes automatically loaded.