How to get the module version number from composer programmatically by Magento 2?

You can fetch/read the current module composer version number from the composer.json file programmatically in Magento 2.

Before fetching the current version, Your module must contain the composer.json file because we are reading the version number from the composer file only.

You can just create a simple Model class and create a function to return the module version.

namespace Rbj\ModuleVersion\Model;

use Magento\Framework\Component\ComponentRegistrar;
use Magento\Framework\Component\ComponentRegistrarInterface;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Exception\ValidatorException;
use Magento\Framework\Filesystem\Directory\ReadFactory;

class ModuleVersionInfo
    public function __construct(
        private readonly ComponentRegistrarInterface $componentRegistrar,
        private readonly ReadFactory                 $readFactory
    ) {

     * @param string $moduleName
     * @return string
     * @throws ValidatorException
     * @throws FileSystemException
    public function getMagentoModuleVersion(string $moduleName): string
        $path = $this->componentRegistrar->getPath(
        $directoryRead = $this->readFactory->create($path);
        $composerJsonData = '';
        if ($directoryRead->isFile('composer.json')) {
            $composerJsonData = $directoryRead->readFile('composer.json');
        $data = json_decode($composerJsonData);

        return !empty($data->version) ? $data->version : '';

You need to pass the Modulename in the function to fetch the current version of the installed module.

$moduleName = 'Magento_Catalog';

The result will be the Module version of the specified module in the function.