title: Conditionally require code in commonJS AMD module
body: <p>I'm trying to write a cross-browser extension for Firefox and Chrome. Firefox uses the commonJS specification and Chrome just lumps everything into the global namespace like a webpage.</p>
  
  <p>In order to be able to write reusable code, I'm trying to use requireJS to lood code in the Chrome extension, that way I can write commonJS modules and have them work in both environments.</p>
  
  <p>I'm running into a problem when I need to conditionally require modules. For example, Firefox provides access to a <code>simple-storage</code> module which you should use to access the local storage. In chrome, I need to use the localStorage API that they provide. So, I've been trying to do this:</p>
  
  <pre><code>// storage.js
  define(function(require, exports, module){
    var store;      
  
    try {
      // This module will only be available in the FF extension.
      store = require('simple-storage').storage
    } catch(error) {
      // If it's not available, we must be in Chrome and we
      // should use the localStorage object.
      store = localStorage
    }
  
    // Use the store object down here.
  });
  </code></pre>
  
  <p>However this doesn't seem to work. When I try to load the Chrome extension I get the following error: </p>
  
  <p><img src="https://i.stack.imgur.com/sMOY8.png" alt="Chrome error"></p>
  
  <p>Is there a better way to require modules with a fallback?</p>