Mapbox-gl-js: Require mapbox-gl in Nuxt.js project

Created on 21 Mar 2017  ·  16Comments  ·  Source: mapbox/mapbox-gl-js

mapbox-gl-js version:

I dedicated a repository to this issue:
https://github.com/martinrisseeuw/vue-mapbox-gl-test

Steps to Trigger Behavior

  1. npm install
  2. npm run dev

Expected Behavior

Load the map correctly

Actual Behavior

In pages/index.vue I require the mapbox-gl library unfortunatly this crashes the application. When saving the project returns an error:

Vue.js error

ReferenceError: self is not defined
    at Object.194 (__vue_ssr_bundle__:3871:29)
    at s (__vue_ssr_bundle__:3474:602)
    at __vue_ssr_bundle__:3474:653
    at Object.192../window (__vue_ssr_bundle__:3867:25)
    at s (__vue_ssr_bundle__:3474:602)
    at __vue_ssr_bundle__:3474:653
    at Object.65.../package.json (__vue_ssr_bundle__:3612:26)
    at s (__vue_ssr_bundle__:3474:602)
    at e (__vue_ssr_bundle__:3474:773)
    at __vue_ssr_bundle__:3474:791

I'm not sure if it's a VUE.JS error or an error that is know to you when you require mapbox-gl (0.34.0).

needs information

Most helpful comment

@amkite Sure!

So, what I did was importing Mapbox at the wrong place. This made Nuxt trying to render it server side.

I created a gist for you to take a look at with the right and wrong example:
https://gist.github.com/martinrisseeuw/96a52024f1f6d9300d1ce3c807badca5

Hope it helped.

All 16 comments

@martinrisseeuw thanks for reporting this issue and for providing the test case.

I am not familiar with Nuxt.js, but from a quick look it appears that it's designed to do server-side rendering. When I look at the example in your test repo, it seems like the error message is already rendered into the HTML--not produced by a script running on the page / in the browser.

Is it possible that the Nuxt.js configuration here is causing mapbox-gl to be loaded server-side, in Node? If so, that is a very likely cause of the error: mapbox-gl-js is not designed to be run outside of a browser.

It was indeed a problem of Mapbox trying to render on the server.

@martinrisseeuw: I bumped into the same problem, could you post an explanation of the solution you found?

Thanks in advance!

@amkite Sure!

So, what I did was importing Mapbox at the wrong place. This made Nuxt trying to render it server side.

I created a gist for you to take a look at with the right and wrong example:
https://gist.github.com/martinrisseeuw/96a52024f1f6d9300d1ce3c807badca5

Hope it helped.

Yeah, it did help, thanks a lot @martinrisseeuw!

Another approach which works and means you can still use import rather than require is to move your Mapbox code out from the page and into a component, and ensure you load that component with ssr: false in the plugins.

nuxt.config.js:

plugins: [
  { src: '~/plugins/map.js', ssr: false }
]

~/plugins/map.js:

import Vue from 'vue'
import Map from '@/components/Map.vue'

Object.keys(components).forEach(key => {
  Vue.component(key, components[key])
})

@martinrisseeuw @andrewharvey were you guys able to get this to work after running nuxt build? It's working properly with both of your techniques when running a dev server with nuxt, but after building the project the map doesn't load and it's throwing several errors (e is not defined). Attached console screenshots. Any ideas?

screen shot 2018-05-02 at 9 11 01 am

screen shot 2018-05-02 at 9 11 44 am

@ajhaupt7 I've only ever used it with nuxt (dev server) and nuxt generate (static file hosting). Both worked fine once I did https://github.com/mapbox/mapbox-gl-js/issues/4463#issuecomment-381104582, so I'm afraid I can't comment on nuxt build (ssr).

@ajhaupt7 we had it running in dev and production. Here you can see how we added it: https://gist.github.com/martinrisseeuw/4943a180632e8ec95a45c5aec4061db8

Thanks guys -- looking like its an issue with webpack, specifically js uglification (https://github.com/mapbox/mapbox-gl-js/issues/1649)

@ajhaupt7 were you able to solve this issue? I'm facing the same Uncaught ReferenceError ... with Nuxt.

Replying to my own question. Solution suggested by @martinrisseeuw worked. My Nuxt build settings needed some work.

// nuxt.config.js
 build: {
    vendor: ['axios', 'mapbox-gl', 'moment'],
    extend (config) {
      config.module.noParse = /(mapbox-gl)\.js$/
    }
}

@jaskiratr @martinrisseeuw thank you guys so much, helped me out 👍

@martinrisseeuw Please print the output of console.log(JSON.stringify(config.module)),
Want to verify what you got in the noParse object..!

Because after adding noParse, my output includes "noParse":{},

thanks.

This is how I required mapbox-gl for a Nuxt project. Maybe it can be useful to somebody:

mounted() {
    const mapboxgl = require('mapbox-gl')
    const map = new mapboxgl.Map({
      accessToken: 'YOUR_TOKEN',
      container: 'map', // <div id="map"></div>
      style: 'mapbox://styles/mapbox/streets-v9',
      center: [-21.9270884, 64.1436456],
      zoom: 13
   })
}

For more info, check out: https://dev.to/mornir/how-to-add-mapbox-to-your-nuxt-static-site-b59

The self is not defined error should be fixed by https://github.com/mapbox/mapbox-gl-js/pull/9749 which was included in v1.11.0.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iamdenny picture iamdenny  ·  3Comments

rasagy picture rasagy  ·  3Comments

aendrew picture aendrew  ·  3Comments

stevage picture stevage  ·  3Comments

mollymerp picture mollymerp  ·  3Comments