Why examples don't work? (a struggle with imports)
Why examples don't work? (a struggle with imports)
On the page https://openlayers.org/en/latest/examples/image-vector-layer.html I copied the HTML code from under the map to /tmp/a.html
and run firefox /tmp/a.html
.
/tmp/a.html
firefox /tmp/a.html
At first two problems appeared easy to fix:
To fix it:
<script>
<script type="module">
<meta charset="UTF-8">
<head></head>
But what to do with the third error?
TypeError: Error resolving module specifier: ol/Map.js
TypeError: Error resolving module specifier: ol/Map.js
I have Firefox 60.0.1.
So, are the HTML codes in the examples meant to be used as I did, or did I misunderstand something?
And what do I need in my code to import Map from ol/Map.js
?
import Map from ol/Map.js
(I tried to reformulate the question, but if I still deserve a negative ranking, please explain why. Thanks)
1 Answer
1
It's because there are some changes due to latest release of OpenLayers (V5.0).
Now samples are based on ES6 modules whereas there was before another way of doing
You can compare the "simple" v4.6.5 sample with "simple" master sample
Using is not enough as it does not solve dependencies when doing import Map from ol/Map.js
import Map from ol/Map.js
There are at least 3 ways of doing:
The usual way to create Openlayers sample using version 5.0.0 is using à bundler like Webpack or Parcel. There is a tutorial for this.
I've also investigated JSPM with this sample
You can always use the old way of doing, like in version 4.6.5 using this other official tutorial without using import
.
import
For solution 1, you can use codesandbox.io to avoid setting a local parcel/webpack environment like illustrated with this tweet.
I know there is a work on progress to refactor the code for samples and I also submitted some suggestions for codesandbox.io e.g https://github.com/openlayers/openlayers/issues/8324
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
ive got the same error trying to help you, this page may not be changed since the update, normally the examples work very well with copy paste
– nalm
Jun 29 at 11:45