[GIS] Trouble with accesing API code: “ReferenceError: esri is not defined”

arcgis-javascript-apiarcgis-servererrorjavascript

I am developing a web map application in the ArcGIS API for Javascript.

I two have ArcGISDynamicMapServiceLayer layers that I am trying to run a simple query task on. The code I am using works in other applications doing the same task with different data.

My problem is, when I load the app (tried both Firefox and Chrome) in a browser, the map itself won't load (indefinite load screen). Console in both browsers shows this error:

ReferenceError: esri is not defined.

Comment #13 on this Esri forum post alludes to the DOM issue, but I need help understanding how to handle the issue. Also, it's possible that the DOM thing is not the issue at all.

So, does anyone have experience with this error? How have you worked around it?

Thanks so much for your input.

UPDATE:
This is the code that is actually throwing the error:

var scalebar = new esri.dijit.Scalebar({
map: map,
attachTo:"bottom-left"
});

Note that I have included the dojo require statement for that class:

dojo.require("esri.dijit.Scalebar");

And if I simply comment out that function, the same undefined error comes with the next esri object class referenced (which also has its appropriate dojo require statement in the doc).

Best Answer

I've seen that happen if you don't put your JavaScript API code into something like an init function like so:

function (init) {
            map = new esri.Map("map", {
                basemap: "streets"
            });
...
}

dojo.ready(init);