PluginManager.register throws error that Plugin is already registered

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) 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

1 „Gefällt mir“

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?

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.

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).

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.