Step-by-Step Guide to Creating and Serving a Mapbox Project Locally

Karol Munoz
3 min readJun 19, 2024

--

In this tutorial, we will create a simple Mapbox project and serve it locally using http-server. Follow the steps below to set up and run your project.

1. Set Up Your Project

1.1 Create a new project directory and navigate into it:

First, open your terminal or command prompt and create a new directory for your project. Then, navigate into this directory.

mkdir mapbox-local && cd mapbox-local

1.2 Initialize a new Node.js project:

Initialize a new Node.js project by creating a package.json file. This file will keep track of your project's dependencies and other configurations.

npm init -y

1.3 Install necessary dependencies:

Install the mapbox-gl package, which is the JavaScript library for Mapbox GL JS.

npm install mapbox-gl

2. Create Your HTML and JavaScript Files

2.1 Create an index.html file:

Create an index.html file in your project directory. This file will contain the basic structure of your web page.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mapbox Local Project</title>
<link href='https://api.mapbox.com/mapbox-gl-js/v2.0.0/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<div id="map"></div>
<script src="main.js"></script>
</body>
</html>

2.2 Create a main.js file:

Create a main.js file in your project directory. This file will contain the JavaScript code to initialize the Mapbox map.

import mapboxgl from 'mapbox-gl';
mapboxgl.accessToken = 'YOUR_MAPBOX_ACCESS_TOKEN'; // Replace with your Mapbox access token
const map = new mapboxgl.Map({
container: 'map', // container ID
style: 'mapbox://styles/mapbox/streets-v11', // style URL
center: [-74.5, 40], // starting position [lng, lat]
zoom: 9 // starting zoom
});

3. Serve Your Project Locally

To view your project in a browser, you need to serve it using a local server. We’ll use http-server for this purpose.

3.1 Install http-server globally:

If you haven’t installed http-server before, install it globally on your machine.

npm install -g http-server

3.2 Run the server from your project directory:

Run http-server from your project directory to serve your files.

http-server

3.3 Open your browser and navigate to http://localhost:8080:

Once the server is running, open your web browser and go to http://localhost:8080. You should see your Mapbox map displayed on the page.

Full Code

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mapbox Local Project</title>
<link href='https://api.mapbox.com/mapbox-gl-js/v2.0.0/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<div id="map"></div>
<script src="main.js"></script>
</body>
</html>

main.js

import mapboxgl from 'mapbox-gl';
mapboxgl.accessToken = 'YOUR_MAPBOX_ACCESS_TOKEN'; // Replace with your Mapbox access token
const map = new mapboxgl.Map({
container: 'map', // container ID
style: 'mapbox://styles/mapbox/streets-v11', // style URL
center: [-74.5, 40], // starting position [lng, lat]
zoom: 9 // starting zoom
});

Conclusion

You have now successfully created a simple Mapbox project and served it locally using http-server. This guide covers the basics, but you can further customize your map by exploring the Mapbox GL JS documentation and adding more features to your project.

--

--

Karol Munoz

Smart cities. Design systems. GIS maps. Graphic, UI, UX Design. Designer. Coder. NYC.