Home  >  Q&A  >  body text

Browserify and @google/earthengine: Package JavaScript modular geospatial data access and analysis into browser-usable files

<p>I'm trying to use Browserify to import Google Earth Engine's Javascript API. </p> <p>I have installed this module: </p> <pre class="brush:php;toolbar:false;">npm install --save-dev @google/earthengine</pre> <p>I created a new main.js file for testing purposes: </p> <pre class="brush:php;toolbar:false;">var md = require('@google/earthengine'); module.exports = MDOutSystems; function MDOutSystems() { this.mdInstance = md; }; MDOutSystems.prototype.data.authenticateViaPrivateKey = function( privateKey, opt_success, opt_error, opt_extraScopes, opt_suppressDefaultScopes) { md.data.authenticateViaPrivateKey(privateKey, opt_success, opt_error, opt_extraScopes, opt_suppressDefaultScopes); }; MDOutSystems.prototype.initialize = function() { md.initialize(); }; MDOutSystems.prototype.Image = function(source) { md.Image(source); }; MDOutSystems.prototype.getInstance = function () { return this.mdInstance; }</pre> <p>(I received a warning that I need to create a d.ts file with <code>declare module '@google/earthengine'</code>)</p> <p>I use the following code to expose the module I created: </p> <pre class="brush:php;toolbar:false;">Browserify main.js --standalone MDOutSystems > google-earth-outsystems.js</pre> <p>However, when I try to call </p> <pre class="brush:php;toolbar:false;">var ee = new MDOutSystems();</pre> <p>I get an error saying "MDOutSystems is not defined". </p> <p>Help. </p> <p>I tried moving main.js into the /node_modules folder and running the browserify command again. This actually resulted in a completely different google-earth-outsystems.js file, but it still didn't work. </p>
P粉208469050P粉208469050453 days ago513

reply all(1)I'll reply

  • P粉545218185

    P粉5452181852023-08-16 00:24:47

    I guess the browser will compress the code and change the function name.

    MDOutSystems() will not be recognized after that.

    Attach your methods to the window object.

    like this:

    function MDOutSystems() {
      this.mdInstance = md;
    };
    
    window.MDOutSystems = MDOutSystems;

    reply
    0
  • Cancelreply