title: Using RequireJS with Graal
body: <p>I am trying to use requireJS with GraalVM (Polyglot API) to load external JS code in my guest language scripts. 
  The way I am going about this is by first running requirejs on the context:</p>
  
  <pre><code>    Context importCtx = Context.create();
  
      String libraryUrl = "file:/require.js";
      Source librarySource = Source.newBuilder("js", new URL(libraryUrl)).build();
  
      importCtx.eval(librarySource);
  </code></pre>
  
  <p>This results in require being added to the JS global, which allows me to use it in the following JS scripts running off that same context. I then simply run my guest script:</p>
  
  <pre><code>    String scriptUrl = "test.js";
      Source scriptSource = Source.newBuilder("js", new URL(scriptUrl)).build();
  
      importCtx.eval(scriptSource);
  </code></pre>
  
  <p>My guest script looks like this:</p>
  
  <pre><code>console.log("start of script");
  
  require(['http://momentjs.com/downloads/moment.js'], function(mom) {
      console.log("inside require function");
      debugger;
      console.log(mom.now();
  });
  
  debugger;
  console.log("end of script");
  </code></pre>
  
  <p>When running this script in a browser it works properly and prints the value of moment.now(). However, when running it off the Polyglot API, the JS runs, require is set correctly, but function(mom){} is never called. Basically I never see the "inside require function" message and the value of moment.now(). </p>
  
  <p>I have a feeling that this is happening because an error is occurring in loading the external resource (I have also tried loading off a file instead of the remote URL with no luck). However I cannot see any errors, even when running off chrome devtools with the inspect option enabled. </p>
  
  <p>What could be the problem?
  Thanks.</p>