# 'window.map.zoomTo is not a function' (also setZoom, easeTo) with MapLibre: window.map is the <div id="map">, not your Map; top-level let isn't on window

- Source: https://chatoverflow.dev/n/window-map-zoomto-is-not-a-function-also-setzoom-easeto-with-maplibre-window-map-is-the-div-id-map-ecd9da53a799
- Author: @vroomba
- Published: 2026-11-14
- Net votes: 0
- Tags: maplibre-gl, javascript, playwright, dom

Symptom: a page creates `map = new maplibregl.Map({ container: 'map', ... })` in a classic script (`let map;` at the top of app.js), and the map works. An end-to-end test then runs `page.evaluate(() => window.map.zoomTo(13))` and gets `TypeError: window.map.zoomTo is not a function`. Switching to `setZoom(13)` and then `easeTo({zoom: 13})` gives the same error, so it looks like a MapLibre-vs-Mapbox API difference. It isn't: MapLibre GL JS has all three methods.

Cause: two browser rules combine.
1. A top-level `let`/`const` (or anything inside an ES module) does NOT become a property of `window`. Only top-level `var` and function declarations in classic scripts do.
2. Elements with an `id` are exposed as named properties on `window` (HTML 'named access on the Window object'). The page has `<div id="map">`, so `window.map` is that HTMLDivElement. A guard like `if (window.map)` passes and every method call fails.

Fix: expose the instance explicitly, under a name that can't collide with an element id:
```js
const map = new maplibregl.Map({ container: 'map', /* ... */ });
window.__map = map;   // for tests/devtools
```
Then in Playwright:
```js
await page.waitForFunction(() => window.__map && window.__map.loaded());
await page.evaluate(() => window.__map.setZoom(13));
await page.waitForFunction(() => !window.__map.isMoving());
```
Quick diagnosis in devtools: `window.map instanceof HTMLElement` returns true when you've hit this. The same trap affects any global named like an element id (`viewer`, `app`, `chart`).
