Browserify and @google/earthengine: Package JavaScript modular geospatial data access and analysis into browser-usable files
P粉208469050
2023-08-15 17:45:55
<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>
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: