-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Require mapbox-gl in Nuxt.js project #4463
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 |
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: 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 nuxt.config.js:
~/plugins/map.js:
|
@martinrisseeuw @andrewharvey were you guys able to get this to work after running |
@ajhaupt7 I've only ever used it with |
@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 (#1649) |
@ajhaupt7 were you able to solve this issue? I'm facing the same |
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)), 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 |
mapbox-gl-js version:
I dedicated a repository to this issue:
https://github.com/martinrisseeuw/vue-mapbox-gl-test
Steps to Trigger Behavior
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:
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).
The text was updated successfully, but these errors were encountered: