# PluginManager.register throws error that Plugin is already registered

**URL:** <https://forum.shopware.com/t/pluginmanager-register-throws-error-that-plugin-is-already-registered/99365>\
**Category:** Shopware 6 (English)\
**Created:** [17. April 2023 um 18:10 UTC](https://forum.shopware.com/t/pluginmanager-register-throws-error-that-plugin-is-already-registered/99365 "2023-04-17T18:10:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom.free](https://avatars.discourse-cdn.com/v4/letter/t/8491ac/32.png) [@tom.free](https://forum.shopware.com/u/tom.free)\
**Post date:** [17. April 2023 um 18:10 UTC](https://forum.shopware.com/t/pluginmanager-register-throws-error-that-plugin-is-already-registered/99365/1 "2023-04-17T18:10:33Z")

</div>

Hi,  
I have a little plugin and when trying to register it in my main.js via

> PluginManager.register(‚MyPlugin‘, MyPlugin, ‚[data-my-plugin]‘);

like the documentation ([Add Custom Javascript | Shopware Documentation](https://developer.shopware.com/docs/guides/plugins/plugins/storefront/add-custom-javascript)) says I get an error that the plugin is already registered.

What I tried so far: I checked the PluginManager and found

- getPlugin(pluginName: String)
- getPluginList()

I tried to create a check if the plugin is already registred with both but failed in each case.

- getPlugin always throw in error in case the plugin had not been registered yet and the strict flag I found in some code sample wasn in my hosted 6.4.20 version anymore
- getPluginList returned undefined for MyPlugin but then complained it was already registred when using below code

> if (!PluginManager.getPluginList()[‚MyPlugin‘]) {  
> console.log(PluginManager.getPluginList()[‚MyPlugin‘]);  
> PluginManager.register(‚MyPlugin‘, MyPlugin, ‚[data-my-plugin]‘);  
> }

> Output was  
> undefined  
> all.js?1681754580919916:3 Uncaught Error: Plugin „MyPlugin“ is already registered.  
> at e.value (all.js?1681754580919916:3:25590)  
> at Function.value (all.js?1681754580919916:3:29360)  
> at Module.G7pA (all.js?1681754580919916:9:2807)

So I am kind of stuck how to prevent the error in a decent way unless I wanna use try catch which seems rather weird at this instance.

Actually I found this to be the case if I bundle the main.js via webpack. If I dont bundle. It seems Shopware does some magic and it works. After watching tutorial videos I had thought one needs to bundle the code?

Thanks  
Tom

---

<div class="post-metadata">

**Author:** ![rwsragh](https://avatars.discourse-cdn.com/v4/letter/r/8dc957/32.png) [@rwsragh](https://forum.shopware.com/u/rwsragh)\
**Post date:** [11. April 2024 um 05:23 UTC](https://forum.shopware.com/t/pluginmanager-register-throws-error-that-plugin-is-already-registered/99365/2 "2024-04-11T05:23:32Z")

</div>

I am facing same problem with a plugin „BrainstNavigation“ , all.js?1712811783:3 Uncaught Error: Plugin „HoverImageChange“ is already registered.  
at h.register (all.js?1712811783:3:17832)  
at p.register (all.js?1712811783:3:20676)  
at 6333 (all.js?1712811783:9:2196)  
at o (all.js?1712811783:4:128)  
at all.js?1712811783:9:10809  
at o.O (all.js?1712811783:4:398)  
at all.js?1712811783:9:10829  
at r (all.js?1712811783:4:1212)  
at all.js?1712811783:9:56

Can any one help me on this?

---

<div class="post-metadata">

**Author:** ![Torbey](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/torbey/32/23706_2.png) [@Torbey](https://forum.shopware.com/u/Torbey)\
**Post date:** [25. April 2024 um 10:41 UTC](https://forum.shopware.com/t/pluginmanager-register-throws-error-that-plugin-is-already-registered/99365/3 "2024-04-25T10:41:21Z")

</div>

Your plugin is already registered somewhere else. Have a look in your project. FIY plugins can also be registered within a theme. I had a case like this where a plugin was registered both in my theme and in a separate plugin. If you only want to register the plugin if it is not yet registered, you can do this with the following code.

```auto
if (!'MyPlugin' in PluginManager.getPluginList()) {
    PluginManager.register('MyPlugin', MyPlugin, '[data-my-plugin]');
}

```

So your approach wasn’t all that wrong. _**PluginManager.getPluginList()**_ returns an object with the installed plugins as keys. Therefore, you can simply check for them with the _ **in** _ operator ([in - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in)).

Don’t forget to run `bin/build-storefront.sh` afterwards and clear the cache if necessary.

Hope I could help you and anyone looking for it.
