Skip to content
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

API access token is required to use Mapbox GL #822

Open
paoe60 opened this issue Jan 21, 2020 · 21 comments
Open

API access token is required to use Mapbox GL #822

paoe60 opened this issue Jan 21, 2020 · 21 comments

Comments

@paoe60
Copy link

paoe60 commented Jan 21, 2020

Tell me please. Why do I receive this error: Error: "An API access token is required to use Mapbox GL. See https://www.mapbox.com/api-documentation/#access-tokens-and-token-scopes"
Any other token doesn't work either

Example: https://stackblitz.com/edit/react-ts-fe73xz

@mklopets
Copy link
Collaborator

Do you only get this on stackblitz or also in your local dev environment? It seems to me that it could somehow have to do with the module resolution. The code itself is correct...

@paoe60
Copy link
Author

paoe60 commented Jan 21, 2020

@mklopets in local dev env too =(

@mklopets
Copy link
Collaborator

you have previously opened issues in this repo – you had code that was at least somewhat working back then – do you get the same issue now when you run that code & setup?

@paoe60
Copy link
Author

paoe60 commented Jan 22, 2020

My code worked well on version 4.6.1. But it stopped on 4.8.2.

Example: https://stackblitz.com/edit/react-ts-6xwwgg (in local env on version 4.6.1 everything works well)

@psmaAaron
Copy link

I'm getting the same problem.

Just trying to get a map to render after react-map-gl didn't fulfill our needs.

@mreeder
Copy link

mreeder commented Feb 28, 2020

I also run into this bug on Meteor stack. Both local dev and in Galaxy. Works fine up to and including version 4.6.2. Version 4.7.0-4.8.2, I get this error. Happy to help troubleshoot as best I can.

@danactive
Copy link

danactive commented Feb 29, 2020

I'm getting the same issue going from react-mapbox-gl@4.6.2 to react-mapbox-gl@4.7.0 without changing mapbox-gl@1.1.1 version.

My working code is here before the upgrade.

PS: I tried to debug this further by changing my package.json to link directly to your git commits but due to your TypeScript configuration the lib folder does not exist in the repo which blocks me

@dsmalicsi
Copy link

I am experiencing the same problem with the latest version. I downgraded and it works perfectly.

Uncaught (in promise) Error: An API access token is required to use Mapbox GL. See https://www.mapbox.com/api-documentation/#access-tokens-and-token-scopes
    at N._makeAPIURL (modules.js:1140)
    at N.normalizeStyleURL (modules.js:1140)
    at i.loadURL (modules.js:1144)
    at r._updateStyle (modules.js:1144)
    at r.setStyle (modules.js:1144)
    at new r (modules.js:1144)
    at ReactMapboxGl.ReactMapboxFactory.ReactMapboxGl.componentDidMount (modules.js:124357)
    at commitLifeCycles (modules.js:26940)
    at commitLayoutEffects (modules.js:29927)
    at HTMLUnknownElement.callCallback (modules.js:7281)

@wchavarria03
Copy link

wchavarria03 commented Mar 3, 2020

I am having the same issue and the only thing I did was update the package to the latest version :/
Can we expect a fix soon?

Last working version was 6.2 after that every version is failing

@gajus
Copy link

gajus commented Mar 4, 2020

@alex3165 Are you aware of this?

@mattJurz
Copy link

mattJurz commented May 7, 2020

Same issue here. Did anyone manage to solve this problem?

@JasonZheng20
Copy link

Same issue still, keep me posted if there was a fix! Downgrading seemed to work for now.

@ndreas
Copy link

ndreas commented Nov 30, 2020

I got bitten by this as well, solved it for mapbox-gl 1.13.0 and react-mapbox-gl 5.1.1 by adding this to my app:

(MapboxGL as any).accessToken = '<accessToken>';

The lib does the same, but this seems to work when the lib doesn't.

@yo-bur
Copy link

yo-bur commented Dec 3, 2020

Confirming @ndreas's fix works for mapbox-gl v1.13.0 and react-mapbox-gl v5.1.1.
Thanks

@mikevb3
Copy link

mikevb3 commented May 1, 2021

im getting the same error using Vite as bundler.

also i had to add this to my vite config:

export default defineConfig({ plugins: [reactRefresh()], resolve: { alias: [ { find: /^react-mapbox-gl/, replacement: "react-mapbox-gl/lib", }, ], }, define: { global: "window", // fix for packages that support both node and browser }, });

EDIT: without that fix, vite won't run, and this error appears, node_modules/react-mapbox-gl/lib-esm/map.js:59:25: error: Cannot assign to import "accessToken" 59 │ MapboxGl.accessToken = accessToken;

@vfonic
Copy link

vfonic commented May 9, 2021

I've finally managed to fix this issue. I'm also using Vite (Rollup) bundler.

This is what worked for me:

// somewhere in your app code before using mapbox:
import * as mapboxgl from 'mapbox-gl';
(mapboxgl as any).accessToken = mapboxAccessToken;
// vite.config.js
export default defineConfig({
  // ...
  resolve: {
    alias: [
      // ...
      {
        find: /^react-mapbox-gl/,
        replacement: 'react-mapbox-gl/lib',
      },
    ],
  },
});

mapbox-gl 1.13.1
react-mapbox-gl 5.1.1

@JonasGruenwald
Copy link

Unfortunately, this didn't work for me with vite.js on those versions or the latest ones, however I was able to set the token like this:

import * as mapboxgl from 'mapbox-gl';
//@ts-ignore
Object.getOwnPropertyDescriptor(mapboxgl, "accessToken").set(`${import.meta.env.VITE_MAPBOX_TOKEN}`);

@brianvandenakker
Copy link

It looks like versions earlier that 7.0 used: mapboxApiAccessToken=YOUR_ACCESS_TOKEN. to set the API key.

v7.0 uses: mapboxAccessToken=YOUR_ACCESS_TOKEN.
As described here[https://github.com/visgl/react-map-gl/blob/29aa2098febf1e6b8bbfd2caecb3745b3d4d82a9/docs/upgrade-guide.md]

That change fixed it for me.

@JonasGruenwald
Copy link

@brianvandenakker I am a bit confused, you're linking to react-map-gl, which I thought was an entirely different library than react-mapbox-gl (this repository), of which the latest version is 5.1.1 ?

@AsaoluElijah
Copy link

Thanks, @brianvandenakker your fix worked very well for me!

@NayoBaez
Copy link

@brianvandenakker Thanks a lot, the documentation you shared helped me fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests