SPFx web part on classic web sites causes uncaught type error
Category
- [x] Bug
Expected or Desired Behavior
The web part should load properly on classic publishing pages (in classic web sites) as it does on modern pages (the development environment).
Observed Behavior
When I try to implement a SPFx web part including React, Redux and some other modules (namely moment, immutable, jQuery and superagent), written in TypeScript, I'm getting an 'Uncaught TypeError: Unexpected anonymous AMD define.':

The error occurs at the point define(factory) in 'branding.o365.js' where moment.js should be included (line 10263):

Steps to Reproduce
The app has been developed and successfully tested on a modern page and wiki page in a modern team site. Even when it was deployed on the production tenant, it worked on these page types. But when the web part is placed on a classic wiki page or publishing page (in a classic team site), we experience the described behavior.
If you need some more detailed information, feel free to ask for it! Tags: #spfx
Thanks for reporting this issue. Could you include the sppkg file that you are using which causes this error? It would help us investigate the issue.
Thanks for your answer. Please find the zipped sppkg file attached. I hope it can help to figure out what the issue is. webpart-beezy-activities.zip
Hello Marco,
I was not able to find branding.o365.js in the package, so I'm wondering how you're loading it on the page. Is it coming from a master page? If so, is it using requirejs to load modules?
Thanks for your reference to the branding.o365.js, I just didn't recognize it before! Now I could have a look at the branding wich is loaded via 'Custom User Action'.
I figured out the following:
- When using our Beezy webpart on an un-branded site collection (no branding.o365.js is loaded), or using the branded site collection without the Beezy webpart, everything works without any error.
- We don't use require.js, but some included modules are defined as anonymous AMD modules (moment.js, magnific-popup, jQuery and some jQuery UI widgets). When I don't include them, it works.
- When I enable browser caching, the webpart works (mostly) as expected, even with AMD modules. But when disabling the cache, the loading procedure crashes in sp-classic-page-assembly…js, although the loading order of the scripts obviously remains the same (I can post some screenshots if needed). See the different error message in the screenshot attached:

How do you load the anonymous AMD modules? From the callstack, it looks like it may be using the global define() that SPFx sets for loading modules as a script. The problem is not on loading the JS files, but eval'ing them. Understanding what's your use case when loading those scripts will help understand how adding SPFx in the page impacts with that.
The AMD modules (moment.js, jQuery, jQueryUI) are concatenated by a gulp action in the branding.o365.js which is injected into the
-tag of the DOM by an UserCustomAction-ScriptLink, so the modules should be in global context. These are used for the branding tasks in the classic page. We don't use require.js explicitly here, but we noticed that require.js is used in the SharePoint page context itself. So it may be that this require.js instance influences our scripts. In the modern webpart, the modules (moment.js, React, Redux, ...) are required by 'import'-statement and bundled by webpack as usual.
We are in the process in rolling out a change in the loader using RequireJS that (as a side effect) will probably fix that problem directly. If you want, contact me at [email protected] and we can sync up and check that it will fix your problem (or otherwise debug it together). As it's something specific with your classic experience, I think it'll be more efficient to do it this way.
Please repoen this issue if you don't see a fix within 3 weeks
Hi,
We are also experiencing this issue. We have a self calling JavaScript method that loads some analytics (a but like Google Analytics does) that we run via a Site scoped UserCustomAction. This has worked for years on classic pages, but, by design, doesn't work on modern pages.
Now, when we put a SPFx Client Side Web Part on a classic wiki page, we get the exact error message shared by the OP.
By the nature of @mpasarin's reply, can I assume that we should see this start to work with the rollout of the change eluded to on the 31 Jul? Do we know when we are likely to see that in our tenants so that we can update our customers?
Regards,
We too are still seeing the same issue. We have a very simple SPFx web part which, when added to a classic SharePoint page, produces the error "Unexpected anonymous AMD define".
It would be good to know whether the change eluded to previously is likely to have been rolled out completely or if there is further debugging and fixing that we must do.
We need this bug resolving very quickly as it is impacting a number of customer deployments.
@mpasarin, do you have a timeline that we can give to our customers of when this will be resolved?
The change rollout had to be stopped due to unforeseen side effects. After fixing those, we are currently rolling out those changes. The expectation should be that in 2-3 weeks the issue should be resolved.
We worked around this issue by not loading our script dependencies in the page via javascript. We packaged everything up into one file and loaded this directly via custom action. The problem arises when script dependencies are loaded "later" asynchronously when SharePoints require.js is active. This seems to happen only when there is an modern webpart on the page. But this approach won't work for all cases.
Regards Tobias
Hello everyone,
Any news about this issue ?
EDIT : I worked around the issue. I set the "define" property to undefined before the script injection, after the script loaded I redefine the "define" property.
Regards
Ibrahim
I haven’t implemented the workaround suggested by Supaibo yet but we are still experiencing this issue. Was anyone else able to solve this without the workaround? Are other people still experiencing this as well?
@mpasarin: Can you confirm that the changes to solve this issue are rolled out?
Regards, Rob
Hi has there been any updates for this? I'm using the pnp javascript loader to load javascript files for branding, analytics etc and get this error with a classic 2016 SP On-premises page with a SPFx webpart.
@sonicbaz Having same issues with the AMD conflict.
@mpasarin any hope of getting a fix pushes to SharePoint 2016 on-prem?
Can anyone confirm this is still an issue with SharePoint Online as this has been open for quite some time... likely fixed in SPO...
@samculver Unfortunately there's no story for updating SPFx in on-prem environments.
SPFx webparts load properly in our default template SPO classic pages.
We have a client though who has a customized template in SPO. They are now encountering this issue in their custom template but not in SPOs classic default template.
Is it possible for the DEV to shed some light on how this was fixed? We'd like to pass this information onto our client.