Magento 2 Get Module Directory Path Programmatically

As per my experience, many times we have to get the location of the module or module directory path while creating the custom module and especially it’s either shipping or payment module. E.g when we wanted to know the exact path of certificate file added to our custom module’s etc folder or .wsdl file of third party module added in our modules.

The following code use to get the exact location of our module on the server.

namespace Vish\Demo\Block;

use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\Module\Dir\Reader;

class DirectoryLocation
	/** @var Reader */
    private $dirReader;

	 * @param Context $context,
	 * @param Reader $dirReader
    public function __construct(
        Context $context,
        Reader $dirReader,
		array $data = []
    ) {
        $this->dirReader = $dirReader;
		parent::__construct($context, $data);

	public function getDirectoryLocation():
	    return $this->dirReader->getModuleDir(

Get directory path on phtml file as showing below.

echo $block->getDirectoryLocation();
// output: website_root_location/app/code/Vish/Demo/etc/

You can call any of the following directories of the module by just changing \Magento\Framework\Module\Dir::MODULE_ETC_DIR with following in the getDirectoryLocation() function.

To get ‘Controller’ directory \Magento\Framework\Module\Dir::MODULE_CONTROLLER_DIR
To get ‘etc’ directory \Magento\Framework\Module\Dir::MODULE_ETC_DIR
To get ‘view’ directory \Magento\Framework\Module\Dir::MODULE_VIEW_DIR
To get ‘Setup’ directory \Magento\Framework\Module\Dir::MODULE_SETUP_DIR
To get ‘i18n’ directory \Magento\Framework\Module\Dir::MODULE_I18N_DIR