1

I'm developing Magento 2 module. My composer.json file contains a dependency ("require") to netresearch/jsonmapper.

I run php composer update in app/code/<vendor>/<module> folder. It downloads jsonmapper package and puts into app/code/<vendor>/<module>/vendor folder.

When I run bin/magento setup:di:compile command it gives the error:

[RuntimeException]
Source class "\Json" for "JsonMapper" generation does not exist.

So my question is how to avoid of code generation for vendor folder?

And if I'm using external dependencies in my Magento 2 module in a completely wrong way, then please point me to the right direction.

1

And if I'm using external dependencies in my Magento 2 module in a completely wrong way, then please point me to the right direction.

Do not run composer install or composer update in app/code for requirements that you need within Magento

Instead, add the requirements to the Magento installation. In the root directory, run:

composer require netresearch/jsonmapper

Preferably with the version constraint that you also have in your module, like:

composer require netresearch/jsonmapper "~1.0"

Alternative

What I prefer for extension development is to develop it within vendor. vendor/<vendor>/<module> is a Git repository, as soon as you installed it once:

  1. create repository with composer.json and registration.php
  2. install it into the Magento project with composer
  3. develop within vendor and use Git as usual
  4. if you updated dependencies of the extension in composer.json:
    1. commit and push
    2. add a version tag
    3. composer update in the Magento project to fetch the dependencies
4
  • Thanks @fabian-schmengler for the explanation. The extension I'm working on is not a standalone one. It's kind of a connector to another system, and it cannot be put to its own repository. However, I want not to bother users with the dependencies needed by the extension. Seems like requiring the users to run composer require ... is not a good idea (I don't want to update the installation manual every time I add new dependency to my extension). Any other alternatives? Jun 21 '17 at 4:31
  • Depends. How do people install your extension if not with composer? Jun 21 '17 at 5:32
  • Just copy extension files to app/code and run setup:upgrade, setup:di:compile. Jun 22 '17 at 11:16
  • Then the composer.json does not have any effect. I don't understand why your extension cannot be provided as composer package. This would be the ideal solution, easy for you and easy for the user. But if you really cannot do anything about it, you need to ship it together with all dependencies: Include them in your extension under lib and provide an autoloader for them. Jun 22 '17 at 11:22
1

And finally I found less or more suitable solution.

It is possible to provide Magento 2 extension as a zip package:

  1. Zip your extension (as usual, composer.json must be in the root of the archive)
  2. Copy it to some folder in the machine where Magento 2 is installed (for ex, /tmp/composer-repos)
  3. Go to Magento 2 root folder and execute composer config repositories.local artifact /tmp/composer-repos. This will add local-folder repository to composer.json of Magento 2. More info
  4. Execute composer require <vendor>/<module>. Composer will find your zip file in /tmp/composer-repos and install it.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.