Incorrectly setting module template variable Getting error: 


  Calling unknown method:
  craft\web\twig\variables\CraftVariable::markAsReadModule()


Trying to use a template variable that I created in my Craft 3 module, I use this module for other things that work as expected but I am running into an issue with this in the template:

{{ craft.markAsReadModule.read(entry.id) }}


In the init method for craft/modules/markasreadmodule/MarkAsReadModule.php I register the variable:

I am using:

use craft\web\twig\variables\CraftVariable;
use modules\markasreadmodule\variables\MarkAsReadModuleVariable;


Then I have this in the init:

Event::on(
    CraftVariable::class,
    CraftVariable::EVENT_INIT,
    function (Event $event) {
        /** @var CraftVariable $variable */
        $variable = $event-&gt;sender;
        $variable-&gt;set('markAsReadModule', MarkAsReadModuleVariable::class);
    }
);


In craft/modules/markasreadmodule/variables/MarkAsReadModuleVariable.php

public function read($entryId)
{
    $service = new MarkAsRead(); // initialize the service class
    $read = $service-&gt;read($entryId);
}


I suspect I am setting the naming convention where I include it in the template and/or in the init method within craft/modules/markasreadmodule/MarkAsReadModule.php
Was running into the same problem today. I was able to access the controller and the service, but none of the variables. Looks like Brad was able to fix the problem but didn't add a final solution. 

I wasn't registering any variables on the init() function. Adding the last line of code registered the variable but did not load it onto every request. 

    // Register our variables
    Event::on(
        CraftVariable::class,
        CraftVariable::EVENT_INIT,
        function (Event $event) {
            /** @var CraftVariable $variable */
            $variable = $event-&gt;sender;
            $variable-&gt;set('formSenderModule', FormSenderModuleVariable::class);
        }
    );


To add it onto every request, I had to add form-sender-module into my bootstrap array in app.php. 

'bootstrap' =&gt; ['form-sender-module']