How to get the module version programmatically by Magento 2?

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

Before fetching the current version, your module contains the composer.json file.

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.