0

I have a simple nodejs project that should load asynchronously the google maps api javascript, i followed this answer https://stackoverflow.com/a/15796543 and my app.js is like this:

var express = require("express"),
    app = express(),
    bodyParser  = require("body-parser"),
    methodOverride = require("method-override");
    https = require("https");
    requirejs = require('requirejs');

requirejs.config({
    waitSeconds : 500,
    isBuild: true,
    paths : {
        'async': 'node_modules/requirejs-plugins/src/async',
    }
});

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(methodOverride());
var router = express.Router();
router.get('/', function(req, res) {
    res.send("Hello World!");
});

requirejs(["async!http://maps.google.com/maps/api/js?key=mykey&sensor=false"], function() {
    console.log(google);
});

app.listen(3000, function() {
    console.log("asd");
});

package.json:

{
 "name": "rest-google-maps-api",
 "version": "2.0.0",
 "dependencies": {
     "express": "^4.7.1",
     "method-override": "^2.1.2",
     "body-parser": "^1.5.1",
     "requirejs": "2.3.3",
     "requirejs-plugins": "1.0.2"
 }
}

i've got always the same error:

ReferenceError: google is not defined

2 Answers2

0

The main issue here is that you are trying to run in Node code that is really meant to be used in a browser.

The async plugin

This plugin needs to be able to add script elements to document and needs window. I see you set isBuild: true in your RequireJS configuration. It does silence the error that async immediately raises if you do not use this flag, but this is not a solution because:

  • isBuild is really meant to be set internally by RequireJS's optimizer (or any optimizer that is compatible with RequireJS), not manually like you are doing.

  • isBuild means to indicate to plugins that they are running as part of an optimization run. However, your code is using the plugin at run time rather than as part of an optimization. So setting isBuild: true is a lie and will result in undesirable behavior. The async plugin is written in such a way that it effectively does nothing if isBuild is true. Other plugins may crash.

Google's Map API

It also expects a browser environment. The very first line I see when I download its code is this:

window.google = window.google || {};

Later in the code there are references to window.document and window.postMessage.

I don't know if it is possible to run the code you've been trying to load from Google in Node. I suspect you'd most likely need something like jsdom to provide a browser-like environment to the API.

Louis
  • 146,715
  • 28
  • 274
  • 320
-1

assuming you did everything else correctly, which I am not testing here. The reason you are getting the error is because you call console.log(google) and there is no google variable. You need to pass google in as a reference in your call back function. This will either get rid of the error, or change the error if you have set up requirejs incorrectly.

requirejs(["async!http://maps.google.com/maps/api/js?key=mykey&sensor=false"], 
function( **google** ) {
    console.log(google);
});

see the requirejs docs http://requirejs.org/docs/node.html#1

sasnyde2
  • 316
  • 2
  • 10