简体   繁体   中英

Mapbox gl js Uncaught ReferenceError: require is not defined

So I installed mapbox gl js, webpack, and browserify which Mapbox gl js requires on mac osx. However, I get Uncaught SyntaxError: Cannot use import statement outside a module error when I try to run the code on localhost.

All the modules are installed in Users/jbk/node-modules directory, and html file is in document folder.

<html>

<head>
    <link href='https://api.mapbox.com/mapbox-gl-js/v1.8.0/mapbox-gl.css' rel='stylesheet' />
</head>


<body>
<div id='map' style='width: 400px; height: 300px;'></div>
<script>


    var mapboxgl = require('mapbox-gl/dist/mapbox-gl.js');

    mapboxgl.accessToken = 'pk.eyIjoic252cGxheWVyIiwiYSI6ImVmcG51V28ifQ.Dfj_Uwi6FXQu4AgmBaAESw';
    var map = new mapboxgl.Map({
        container: 'map',
        style: 'mapbox://styles/mapbox/streets-v11'
    });
</script>

</body>

</html>

您可以使用 mapbox 的 CDN 而不是导入 mapbox。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM