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.
2 Answers 2
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:
- create repository with
composer.jsonandregistration.php - install it into the Magento project with composer
- develop within
vendorand use Git as usual - if you updated dependencies of the extension in
composer.json:- commit and push
- add a version tag
composer updatein the Magento project to fetch the dependencies
-
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?Rustem Zinnatullin– Rustem Zinnatullin2017年06月21日 04:31:08 +00:00Commented Jun 21, 2017 at 4:31 -
Depends. How do people install your extension if not with composer?Fabian Schmengler– Fabian Schmengler2017年06月21日 05:32:40 +00:00Commented Jun 21, 2017 at 5:32
-
Just copy extension files to
app/codeand runsetup:upgrade,setup:di:compile.Rustem Zinnatullin– Rustem Zinnatullin2017年06月22日 11:16:20 +00:00Commented Jun 22, 2017 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
liband provide an autoloader for them.Fabian Schmengler– Fabian Schmengler2017年06月22日 11:22:28 +00:00Commented Jun 22, 2017 at 11:22
And finally I found less or more suitable solution.
It is possible to provide Magento 2 extension as a zip package:
- Zip your extension (as usual,
composer.jsonmust be in the root of the archive) - Copy it to some folder in the machine where Magento 2 is installed (for ex,
/tmp/composer-repos) - Go to Magento 2 root folder and execute
composer config repositories.local artifact /tmp/composer-repos. This will add local-folder repository tocomposer.jsonof Magento 2. More info - Execute
composer require <vendor>/<module>. Composer will find your zip file in /tmp/composer-repos and install it.