How to fix Leaflet Map container not found error with JavaScript?

To fix Leaflet Map container not found error with JavaScript, we should make sure we call L.map after the map container element is added.

For instance, we write

<div id="leafletmap"></div>

to add the map container element.

Then we call L.map('leafletmap') to render the map in the div.