title: Electron: loading an external script and passing it into a function
body: <p>I am building an Atom Electron app. Right now I have this in the preload.js of one of my webviews:</p>
  
  <pre><code>var { requireTaskPool } = require('electron-remote');
  var work = '';
  var _ = require('lodash');
  
  work = requireTaskPool(require.resolve('./local/path/to/js/file.js'));
  
  
  function scriptRun() {
    console.log('Preload: Script Started');
    // `work` will get executed concurrently in separate background processes
    // and resolve with a promise
    _.times(1, () =&gt; {
      work(currentTab).then(result =&gt; {
        console.log(`Script stopped. Total time running was ${result} ms`);
      });
    });
  }
  module.exports = scriptRun;
  scriptRun();
  </code></pre>
  
  <p>It gets a <strong>local</strong> script and then executes it in a background process.</p>
  
  <p>I want to do the same exact thing, except I want to retrieve the script from an external source like so</p>
  
  <pre><code>work = requireTaskPool(require.resolve('https://ex.com/path/to/js/file.js'));
  </code></pre>
  
  <p>When I do this, I get errors like:</p>
  
  <pre><code>Uncaught Error: Cannot find module 'https://ex.com/path/to/js/file.js'
  </code></pre>
  
  <p>How can I load external scripts? And then use the loaded scripts with my <code>work</code> function. My feeling is that require only works with local files. If AJAX is the answer, can I see an example of how to get a script, then pass it into my <code>work</code> without executing it prior?</p>