# \[Error\] Module Manager Error on script environment initialization

**URL:** <https://forum.inductiveautomation.com/t/error-module-manager-error-on-script-environment-initialization/34119>\
**Category:** Module Development\
**Created:** [April 22, 2020, 2:34pm UTC](https://forum.inductiveautomation.com/t/error-module-manager-error-on-script-environment-initialization/34119 "2020-04-22T14:34:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrea.morando](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/andrea.morando/32/8039_2.png) [@andrea.morando](https://forum.inductiveautomation.com/u/andrea.morando)\
**Post date:** [April 22, 2020, 2:34pm UTC](https://forum.inductiveautomation.com/t/error-module-manager-error-on-script-environment-initialization/34119/1 "2020-04-22T14:34:48Z")

</div>

Dear all,  
I have developed some custom modules and loaded them in a 8.0.7 gateway instance.  
Every time the gateway service restarts, I get the following Module Manager Error in the gateway logs:

**Module _xyz_ failed to initialize script environment.**

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/2X/9/903f51628cf30e87f6135030cabf710bd9ff2b27.png)

After that, I need to restart all the custom modules manually.  
Where am I wrong? Are there some workarounds to avoid that behaviour?

Thanks a lot.  
Andrea

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [April 22, 2020, 2:36pm UTC](https://forum.inductiveautomation.com/t/error-module-manager-error-on-script-environment-initialization/34119/2 "2020-04-22T14:36:15Z")

</div>

One of the parameters your `GatewayHook` class is passing to `addScriptModule` is probably `null`. `obj` maybe?

---

<div class="post-metadata">

**Author:** ![andrea.morando](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/andrea.morando/32/8039_2.png) [@andrea.morando](https://forum.inductiveautomation.com/u/andrea.morando)\
**Post date:** [April 22, 2020, 3:07pm UTC](https://forum.inductiveautomation.com/t/error-module-manager-error-on-script-environment-initialization/34119/3 "2020-04-22T15:07:10Z")

</div>

Yeah, you are right!  
The `addScriptModule` method receives a `null` object instance as input parameter, because that object (i.e. the “real” module class) is created in the `startup` method of the `GatweayHook` class, whereas the `initializeScriptManager` is called before the startup phase.

Where is it better to create the `obj` object? Maybe in the `setup` (the class needs the gateway context in its constructor method)?

Best,  
Andrea

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [April 22, 2020, 3:15pm UTC](https://forum.inductiveautomation.com/t/error-module-manager-error-on-script-environment-initialization/34119/4 "2020-04-22T15:15:04Z")

</div>

`initializeScriptManager` is called after `setup`.

It’s common for these script objects to need the `GatewayContext`, so you can store it in a field in your hook during the `setup` call and then reference it during `initializeScriptManager` or create the object during `setup` instead.

---

<div class="post-metadata">

**Author:** ![andrea.morando](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/andrea.morando/32/8039_2.png) [@andrea.morando](https://forum.inductiveautomation.com/u/andrea.morando)\
**Post date:** [April 22, 2020, 3:16pm UTC](https://forum.inductiveautomation.com/t/error-module-manager-error-on-script-environment-initialization/34119/5 "2020-04-22T15:16:28Z")

</div>

Thank you very much for your clarifications, Kevin!
