0

Background

RequireJS is a dependancy for another package I want to use in a Visualforce page. I have uploaded RequireJS as a static resource.

<apex:page showHeader="false" standardStylesheets="false">

    <apex:includeScript value="{!$Resource.require }" />

</apex:page>

But when I run the above code, I get this error:

Uncaught Error: Mismatched anonymous define() module: function(k)...

Questions

  1. is it possible to use RequireJS in a visualforce page?
  2. if yes, what am I doing wrong?
  3. if yes, how do I fix it?
2

is it possible to use RequireJS in a visualforce page?

Yes.

if yes, what am I doing wrong?

You have to delay loading the script until the page is ready.

if yes, how do I fix it?

Specify the appropriate attribute:

<apex:includeScript value="{!$Resource.require }" loadOnReady="true" />

Note that the only way I could get this to work is to also load all future scripts using this same syntax. Do not use script tags or you may get unexpected behavior.

  • Is it possible to get RequireJS to work in a Lightning component? – Robs Apr 17 at 8:31
  • @Robs requireJS doesn't seem to be Lightning-compatible out of the box, but it could be patched? I really don't see any point, though, since we have ltng:require. – sfdcfox Apr 17 at 10:07
  • Does ltng:require work as a AMD loader, like this? – Robs Apr 17 at 10:13
  • 1
    @Robs Strictly speaking, no. But you can browserify the code and upload the results. These should be Lightning-compatible. – sfdcfox Apr 17 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.