
Security News
Insecure Agents Podcast: Certified Patches, Supply Chain Security, and AI Agents
Socket CEO Feross Aboukhadijeh joins Insecure Agents to discuss CVE remediation and why supply chain attacks require a different security approach.
nuxt-mapbox
Advanced tools

nuxt-mapbox & mapbox-gl dependencies to your project# Using pnpm
pnpm add -D nuxt-mapbox mapbox-gl
# Using yarn
yarn add --dev nuxt-mapbox mapbox-gl
# Using npm
npm install --save-dev nuxt-mapbox mapbox-gl
nuxt-mapbox to the modules section of nuxt.config.tsexport default defineNuxtConfig({
modules: [
'nuxt-mapbox'
]
})
mapbox section of nuxt.config.tsexport default defineNuxtConfig({
modules: [
'nuxt-mapbox'
],
mapbox: {
accessToken: '{API_KEY}'
}
})
View the Mapbox GL JS Docs for reference.
Map instances are created with components. You can provide all the options through component props
Example:
<MapboxMap
map-id="{ID}"
style="position: absolute; top: 0; bottom: 0; left: 250px; width: 500px;"
:options="{
style: 'mapbox://styles/mapbox/light-v11', // style URL
center: [-68.137343, 45.137451], // starting position
zoom: 5 // starting zoom
}"
>
You can add Layers & Controls by nesting their components inside the Map
Examples:
<MapboxMap>
<MapboxSource
source-id="geojson"
:source="{
type: 'geojson',
data: '/test.geojson'
}"
/>
<MapboxLayer
source-id="geojson"
:layer="{
source: 'geojson',
id: 'geojson-layer',
type: 'fill'
}"
/>
</MapboxMap>
<MapboxMap>
<MapboxLayer
source-id="{ID}"
:source="{
type: 'geojson',
data: '/test.geojson'
}"
:layer="{
source: '{ID}',
id: 'geojson-layer',
type: 'fill'
}"
/>
<MapboxFullscreenControl />
</MapboxMap>
For map instances to be persistent across routes, keepalive must be set to true in nuxt.config.ts.
This is done by default, but you can disable it by setting keepalive to false
All Map events are accessible directly through the component (With full Typescript support!)
View a list of events in the Mapbox Docs
Example:
<MapboxMap
...
@load="exampleFunction"
@click="exampleFunction"
@resize="exampleFunction"
>
You can access events directly on layers as well
Example:
<MapboxLayer
...
@click="exampleFunction"
>
You can have a popup linked to a marker by simply nesting the popup component inside the marker. Example:
<MapboxDefaultMarker
marker-id="marker1"
:options="{}"
:lnglat="[110, 5]"
>
<MapboxDefaultPopup
popup-id="popup1"
:lnglat="[100, 0]"
:options="{
closeOnClick: false
}"
>
<h1 class="test">
Hello World!
</h1>
</MapboxDefaultPopup>
</MapboxDefaultMarker>
You can access the map instance with the useMapbox composable. You must provide the map id.
The map instance will not be available until the page is fully loaded, so you must access it through a callback
useMapbox(mapId, (map) => {
// Do whatever with map here
})
You can access the map instance ref directly with useMapboxInstance
NOTE: The map instance will be null until is initialized so you cannot access it directly on setup. Use a watcher as shown or useMapbox instead:
const map = useMapboxInstance(mapId)
watch(map, () => {
if (map.value)
// Do whatever with map
})
While it is recommended to use the default components, making your own is easy with the built in composables!
You can use defineMapboxPopup & defineMapboxMarker for custom marker & popup components
By passing a template ref you can put custom html directly into your component
Examples:
const popup = defineMapboxPopup(popupId, options, templateRef)
popup?.setLngLat(lnglat)
NOTE: Because of the way markers are implemented in Mapbox, if passing a template ref to defineMapboxPopup you have to define properties in a callback like so:
const markerRef = defineMapboxMarker(markerId, options, templateRef, (marker) => {
marker.setLngLat([110, 6])
})
You can make your own control with the defineMapboxControl composable.
Example:
useMapbox(mapID, (map) => {
if (htmlRef.value) {
const control = defineMapboxControl((_map) => {
return htmlRef.value as HTMLElement;
}, (map) => {})
map.addControl(control);
}
})
If you would like to make your own map component, you can use defineMapboxInstance
Example:
// NOTE: Map instance will be null on server & until it is loaded on client
const map = defineMapboxInstance(MAP_DIV_ID, options);
# Install dependencies
npm install
# Generate type stubs
npm run dev:prepare
# Develop with the playground
npm run dev
# Build the playground
npm run dev:build
# Run ESLint
npm run lint
# Run Vitest
npm run test
npm run test:watch
# Release new version
npm run release
FAQs
Elegant Mapbox GL JS integration with Nuxt
The npm package nuxt-mapbox receives a total of 1,937 weekly downloads. As such, nuxt-mapbox popularity was classified as popular.
We found that nuxt-mapbox demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh joins Insecure Agents to discuss CVE remediation and why supply chain attacks require a different security approach.

Security News
Tailwind Labs laid off 75% of its engineering team after revenue dropped 80%, as LLMs redirect traffic away from documentation where developers discover paid products.

Security News
The planned feature introduces a review step before releases go live, following the Shai-Hulud attacks and a rocky migration off classic tokens that disrupted maintainer workflows.