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

Uncaught ReferenceError: y is not defined

Open UncleChenna opened this issue 4 years ago • 25 comments

This error occurs in production but works well in development

UncleChenna avatar Jan 14 '21 14:01 UncleChenna

Workaround from here https://github.com/mapbox/mapbox-gl-js/issues/10173#issuecomment-753662795

Install worker-loader to deps if you using CRA and add the following to the start of your code

// @ts-ignore
// eslint-disable-next-line import/no-webpack-loader-syntax
mapboxgl.workerClass = require("worker-loader!mapbox-gl/dist/mapbox-gl-csp-worker").default;

ttrubel avatar Jan 20 '21 16:01 ttrubel

@ttrubel

Can you share more details about this workaround? Made npm i webpack-loader and after tried to add

// @ts-ignore
// eslint-disable-next-line import/no-webpack-loader-syntax
mapboxgl.workerClass = require("worker-loader!mapbox-gl/dist/mapbox-gl-csp-worker").default;

but it doesn't compile well. Fails with Module not found: Can't resolve 'worker-loader' in projects/hot-app/src/components/shared/Map

8L4V4 avatar Jan 21 '21 22:01 8L4V4

@Slava-fe-mjstk I faced the same issue and realized that it is "worker-loader" that needs to be used instead of "webpack-loader".

Try npm i worker-loader


I will say that even with the worker-loader dependency installed, the map isn't rendering on my Netlify build.

Maybe there is something else I'm missing in my implementation @ttrubel ?

Code Implementation --> https://github.com/reMRKableDev/dance-specific-talks/blob/master/src/components/Map/configs/index.js

Live App --> https://ds-talks.netlify.app/map

reMRKableDev avatar Jan 21 '21 23:01 reMRKableDev

@Slava-fe-mjstk I was wrong, shame on me :) it's a worker-loader, not webpack-loader

@reMRKableDev I see the app is working, hope you figured out it

ttrubel avatar Jan 26 '21 06:01 ttrubel

@ttrubel I ended up using react-leaflet instead to fix the issue. I'm closing the issue on here since I can't seem to figure out a way around it.

Thanks for everything so far though

reMRKableDev avatar Jan 26 '21 14:01 reMRKableDev

@ttrubel It worked for me. Thanks

jhay-25 avatar Feb 11 '21 11:02 jhay-25

I would close this issue since the aformentioned webpack-loader stuff seems to work (at least for most). This issue is upstream and is due to mapbox switching to ES6 bundle: https://github.com/mapbox/mapbox-gl-js/issues/10173

agusterodin avatar Feb 19 '21 16:02 agusterodin

I encounter the issue, too. I solved it with craco without changing source code.

First, install craco

Then, create craco.config.js and set ignore option for babel as below:

module.exports = {
  babel: {
    loaderOptions: {
      ignore: ["./node_modules/mapbox-gl/dist/mapbox-gl.js"],
    },
  },
};

doraeric avatar Apr 24 '21 18:04 doraeric

I encounter the issue, too. I solved it with craco without changing source code.

First, install craco

Then, create craco.config.js and set ignore option for babel as below:

module.exports = {
  babel: {
    loaderOptions: {
      ignore: ["./node_modules/mapbox-gl/dist/mapbox-gl.js"],
    },
  },
};

It works like a charm! Thank you @doraeric!!

ottojimb avatar May 03 '21 22:05 ottojimb

I encounter the issue, too. I solved it with craco without changing source code. First, install craco Then, create craco.config.js and set ignore option for babel as below:

module.exports = {
  babel: {
    loaderOptions: {
      ignore: ["./node_modules/mapbox-gl/dist/mapbox-gl.js"],
    },
  },
};

It works like a charm! Thank you @doraeric!!

I'm having the same issue, but this isn't working for me :(

Fredrikwide avatar May 13 '21 10:05 Fredrikwide

@Fredrikwide the craco fix doesn't work for me either. @Fredrikwide did you find anything?

KevinColemanInc avatar Jun 27 '21 15:06 KevinColemanInc

This may help? https://github.com/visgl/react-map-gl/issues/1266

tannerhallman avatar Jun 30 '21 17:06 tannerhallman

module.exports = {
  babel: {
    loaderOptions: {
      ignore: ["./node_modules"],
    },
  },
};

As a temporary fix, ignoring all of the node modules resolved the issue :-/

KevinColemanInc avatar Jul 01 '21 14:07 KevinColemanInc

I actually had to switch to google maps because I could not find a workaround that worked without removing this from transpile.

tannerhallman avatar Jul 14 '21 22:07 tannerhallman

I encounter the issue, too. I solved it with craco without changing source code.

First, install craco

Then, create craco.config.js and set ignore option for babel as below:

module.exports = {
  babel: {
    loaderOptions: {
      ignore: ["./node_modules/mapbox-gl/dist/mapbox-gl.js"],
    },
  },
};

Could you please explain a lil bit more ... am very new to coding but I want to display this mapbox style map using netlify.. I have doubt on how to set that ignore option

Jayanth9710 avatar Sep 07 '21 14:09 Jayanth9710

I encounter the issue, too. I solved it with craco without changing source code. First, install craco Then, create craco.config.js and set ignore option for babel as below:

module.exports = {
  babel: {
    loaderOptions: {
      ignore: ["./node_modules/mapbox-gl/dist/mapbox-gl.js"],
    },
  },
};

Could you please explain a lil bit more ... am very new to coding but I want to display this mapbox style map using netlify.. I have doubt on how to set that ignore option

I noticed that they change their doc link, so it's broken. Here's the new link about ignore option from mapbox. You can find all babel options here. However, I just follow the instruction from mapbox's document, so I can't tell why should the option be set.

I don't know why craco doesn't work for some people, maybe there is different setup? I tried today, it's still work. I create a project with create-react-app, add sample code from README, change to craco, and it works! hello-world.zip This is the zip I produced with the steps above, hope this may help.

doraeric avatar Sep 08 '21 17:09 doraeric

module.exports = {
  babel: {
    loaderOptions: {
      ignore: ["./node_modules"],
    },
  },
};

As a temporary fix, ignoring all of the node modules resolved the issue :-/

This was the only thing that worked for me. But I'm trying to understand why, it seems like everything points to the problem being in the mapbox dependency, why isn't ignoring that enough?

chriszrc avatar Sep 25 '21 01:09 chriszrc

module.exports = {
  babel: {
    loaderOptions: {
      ignore: ["./node_modules"],
    },
  },
};

As a temporary fix, ignoring all of the node modules resolved the issue :-/

This was the only thing that worked for me. But I'm trying to understand why, it seems like everything points to the problem being in the mapbox dependency, why isn't ignoring that enough?

Does my hello-world.zip work for you? I have tested it and it works on my computer, so if it doesn't work on your pc, maybe the reason is from environment, like node.js version, system, etc.

doraeric avatar Sep 25 '21 04:09 doraeric

Working for me now by editing craco.config.js to tell babel-loader to ignore mapbox-gl from transpilation. Working config:

module.exports = {
  babel: {
    loaderOptions: {
      ignore: ['./node_modules/mapbox-gl/dist/mapbox-gl.js'],
    },
  },
};

My package.json has:

"dependencies": {
  "mapbox-gl": "^2.5.1",
  "react-map-gl": "^6.1.17",
},
// ...
"browserslist": {
  "production": [
    ">0.2%",
    "not dead",
    "not op_mini all"
  ],
  "development": [
    "last 1 chrome version",
    "last 1 firefox version",
    "last 3 safari version"
  ]
},

I put an answer up on SO with the same solution https://stackoverflow.com/questions/66421921/react-map-gl-cannot-find-variable-on-production-build/70060851#70060851

ashnewport avatar Nov 22 '21 04:11 ashnewport

Editing craco.config.js worked for me!

Coreball avatar Jan 22 '22 03:01 Coreball

Editing package.json worked for me. I'm using maplibre-gl

"browserslist": {
    "production": [
      "defaults",
      "not ie 11"
    ],
  },

check this: https://docs.mapbox.com/mapbox-gl-js/guides/install/#targeting-transpilation-to-es6-with-browserslist

sanchezzzs avatar Sep 29 '22 18:09 sanchezzzs

Editing package.json worked for me. I'm using maplibre-gl

"browserslist": {
    "production": [
      "defaults",
      "not ie 11"
    ],
  },

check this: https://docs.mapbox.com/mapbox-gl-js/guides/install/#targeting-transpilation-to-es6-with-browserslist

I can confirm this. My previous setting was not working: "browserslist": { "production": [ ">0.2%", "not dead", "not op_mini all" ], },

tvonessen avatar Oct 23 '22 22:10 tvonessen

I encounter the issue, too. I solved it with craco without changing source code. First, install craco Then, create craco.config.js and set ignore option for babel as below:

module.exports = {
  babel: {
    loaderOptions: {
      ignore: ["./node_modules/mapbox-gl/dist/mapbox-gl.js"],
    },
  },
};

Could you please explain a lil bit more ... am very new to coding but I want to display this mapbox style map using netlify.. I have doubt on how to set that ignore option

I noticed that they change their doc link, so it's broken. Here's the new link about ignore option from mapbox. You can find all babel options here. However, I just follow the instruction from mapbox's document, so I can't tell why should the option be set.

I don't know why craco doesn't work for some people, maybe there is different setup? I tried today, it's still work. I create a project with create-react-app, add sample code from README, change to craco, and it works! hello-world.zip This is the zip I produced with the steps above, hope this may help.

did not work for me, I am really surprised no official fix for this.

emekaokoli avatar Dec 08 '22 16:12 emekaokoli

{ "production": [ "defaults", "not ie 11" ],

Worked for me

mohnark avatar Mar 05 '23 20:03 mohnark

We're using react-mapbox-gl with maplibre-gl in a CRA + TypeScript app, here's a modified version of https://github.com/mapbox/mapbox-gl-js/issues/10173#issuecomment-793462984 that worked for us (nothing else did):

// craco.config.js
module.exports = {
  babel: {
    loaderOptions: {
      ignore: ["./node_modules/maplibre-gl/dist/maplibre-gl.js"],
    },
  },

agurtovoy avatar Apr 14 '23 01:04 agurtovoy