Search code examples
liferayliferay-auiliferay-7

liferay 7 - Mismatched anonymous define()


I'm trying to include sockjs (v1.1.2) in my liferay 7 portlet, but I'm receiving this warning:

Mismatched anonymous define() module: function ()...

and then the library isn't loaded.

I directly included the .js file in my liferay-portlet.xml like this:

<header-portlet-javascript>/common/js/sockjs.js</header-portlet-javascript>

I don't have any idea to make it work


Solution

  • Looks like you are running into an issue that's been fixed by LPS-68298. If you are using Liferay 7.0 DXP SP2+ or Liferay 7.0 CE GA4+ you can simply open the administration sidebar and select Control Panel > Configuration > System Settings > Foundation > JavaScript Loader > Uncheck Expose Global. If you aren't using one of the above Liferay versions, you need to follow the advice of this answer and disable the AMD loader before loading your script, then re-enable the AMD loader. Alternatively, you can fix this by manually editing your JS file. Change any text in /common/js/sockjs.js like:

    typeof define == 'function'
    

    To:

    false && typeof define == 'function'
    

    This will stop the file from being loaded by Liferay's AMD loader.