react-mapbox-gl icon indicating copy to clipboard operation
react-mapbox-gl copied to clipboard

API access token is required to use Mapbox GL

Open paoe60 opened this issue 5 years ago • 21 comments

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

paoe60 avatar Jan 21 '20 13:01 paoe60

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...

mklopets avatar Jan 21 '20 14:01 mklopets

@mklopets in local dev env too =(

paoe60 avatar Jan 21 '20 15:01 paoe60

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?

mklopets avatar Jan 22 '20 06:01 mklopets

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)

paoe60 avatar Jan 22 '20 08:01 paoe60

I'm getting the same problem.

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

psmaAaron avatar Feb 10 '20 01:02 psmaAaron

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.

mreeder avatar Feb 28 '20 14:02 mreeder

I'm getting the same issue going from [email protected] to [email protected] without changing [email protected] 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

danactive avatar Feb 29 '20 01:02 danactive

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)

dsmalicsi avatar Feb 29 '20 12:02 dsmalicsi

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

wchavarria03 avatar Mar 03 '20 19:03 wchavarria03

@alex3165 Are you aware of this?

gajus avatar Mar 04 '20 08:03 gajus

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

mattJurz avatar May 07 '20 13:05 mattJurz

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

JasonZheng20 avatar Jul 30 '20 00:07 JasonZheng20

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.

ndreas avatar Nov 30 '20 11:11 ndreas

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

yo-bur avatar Dec 03 '20 00:12 yo-bur

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;

mikevb3 avatar May 01 '21 12:05 mikevb3

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

vfonic avatar May 09 '21 11:05 vfonic

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}`);

JonasGruenwald avatar Mar 23 '22 12:03 JonasGruenwald

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.

brianvandenakker avatar Mar 26 '22 16:03 brianvandenakker

@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 ?

JonasGruenwald avatar Mar 26 '22 16:03 JonasGruenwald

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

AsaoluElijah avatar Jun 15 '22 17:06 AsaoluElijah

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

NayoBaez avatar Sep 16 '22 20:09 NayoBaez