title: Loading Highcharts via shim using RequireJS and maintaining jQuery dependency
body: <p>I'm attempting to load the Highcharts library using a shim in RequireJS.  However, when Highcharts loads, it throws an exception because it can't access the jQuery methods it depends on.</p>
  
  <p>The require config looks like so:</p>
  
  <pre><code>require.config({
      baseUrl: "js",
  
      shim: {
  
          'libs/highcharts/highcharts.src.js': {
              deps: ['jquery'],
              exports: function(jQuery)
              {
                  this.HighchartsAdapter = jQuery;
  
                  return this.Highcharts;
              }
          }
      }
  });
  </code></pre>
  
  <p>The exception that is thrown is:</p>
  
  <pre><code>Uncaught TypeError: undefined is not a function
  </code></pre>
  
  <p>and is in regards to this line:</p>
  
  <pre><code>dataLabels: merge(defaultLabelOptions, {
  </code></pre>
  
  <p>The issue is the <code>merge</code> call, which eventually maps itself back to jQuery (or some other adapter that Highcharts supports; but I'm just using jQuery).</p>
  
  <p>I'm not sure exactly how to make sure Highcharts gets access to jQuery using RequireJS and shim.</p>
  
  <p>Has anyone used RequireJS and Highcharts together before?  I guess the issue isn't specific to highcharts, but any library that has other sorts of dependencies.</p>
  
  <p>Thanks in advance for any advice or points to the correct direction!</p>
  
  <p><strong>To add further context,  in hopes that someone who is familiar with require.js or shims will be able to help without having to be too intimately familiar with highcharts, here's some source that sets up this <code>merge</code> method in Highcharts</strong></p>
  
  <pre><code>var globalAdapter = win.HighchartsAdapter,
  adapter = globalAdapter || {},
  
  // Utility functions. If the HighchartsAdapter is not defined, 
  // adapter is an empty object
  // and all the utility functions will be null. In that case they are 
  // populated by the
  // default adapters below.
  
  // {snipped code}
  
  merge = adapter.merge
  
  // {snipped code}
  
  if (!globalAdapter &amp;&amp; win.jQuery) {
      var jQ = win.jQuery;
  
      // {snipped code}
  
      merge = function () {
          var args = arguments;
          return jQ.extend(true, null, args[0], args[1], args[2], args[3]);
      };
  
      // {snipped code}
  }
  </code></pre>
  
  <p>The <code>win</code> object is a reference set up to <code>window</code> at the beginning of the script.  So, I thought adding <code>window.jQuery = jQuery;</code> to the export method on the shim would result in highcharts picking up the jQuery reference; but it didn't.</p>
  
  <p>Again, any insight, info, advice, or heckles would be appreciated at this point - I'm at a complete loss, and starting to question whether trying to implement and AMD package system in browser javascript is even worth it.</p>
  
  <hr>
  
  <p>After accepting the answer from <em>pabera</em> below I thought it appropriate to update my question to reflect how his answer helped my solution (though, it's basically his answer).</p>
  
  <p>RequireJS uses "paths" to find libs that aren't "AMD" supported and loads them on your page.  the "shim" object allows you to define dependencies for the libraries defined in paths.  The dependencies must be loaded before requirejs will try to load the dependent script.</p>
  
  <p>The exports property provides a mechanism to tell requirejs how to determine if the library is loaded.  For core libs like jquery, backbone, socketio, etc they all export some window level variable (<code>Backbone</code>, <code>io</code>, <code>jQuery</code> and <code>$</code>, etc).  You simply provide that variable name as the <code>exports</code> property, and requirejs will be able to determine when the lib is loaded.</p>
  
  <p>Once your definitions are done, you can use <code>requirejs</code>' <code>define</code> function as expected.</p>
  
  <p>Here's my example <code>require.config</code> object:</p>
  
  <pre><code>require.config({
      baseUrl: "/js/",
  
      paths: {
          jquery: 'jquery',
          socketio: 'http://localhost:8000/socket.io/socket.io', //for loading the socket.io client library
          highcharts: 'libs/highcharts/highcharts.src',
          underscore: 'libs/underscore',
          backbone: 'libs/backbone'
      },
  
      shim: {
          jquery: {
              exports: 'jQuery'
          },
  
          socketio: {
              exports: 'io'
          },
  
          underscore: {
              exports: '_'
          },
  
          backbone: {
              deps: ['jquery', 'underscore'],
              exports: 'Backbone'
          },
  
          highcharts: {
              deps: ['jquery'],
              exports: 'Highcharts'
          }
      }
  });
  </code></pre>
  
  <p>As <em>pabera</em> mentioned before, this is for <code>Require.JS</code> version <code>2.0.1</code>.</p>
  
  <p>I hope someone gets some use out of this; I know it road blocked me for a little while; so hopefully we kept you from banging your head into the same spot in the wall that we did, by posting this.</p>