Uncaught ReferenceError: _createClass is not defined (after transpiling with Babel)
--- π΄ EDIT from a gl-js maintainer π΄ ---- We've just opened #10565 do go over some of the decisions of why we moved to ES6, and also summarizing the solutions that everyone has contributed in this thread. Would love for the discussion to be continued there. ππ½ --- π΄ END EDITπ΄-----
mapbox-gl-js version: 2.0.0
browser: Google Chrome Version 87.0.4280.67 (Official Build) (x86_64)
Steps to Trigger Behaviour
I'm trying to build a library of Vue components and one component uses mapbox-gl. I created a test project to showcase the issue.
- Create a Vue library (vue create library)
- Add mapbox-gl to the library in a component
- Build the library
- Use the library in a Vue application
When I build and use it I have an error message saying:
Uncaught ReferenceError: _createClass is not defined
Link to Demonstration
https://gitlab.com/antoinealej/test-lib-mapbox-gl-build
Expected Behaviour
Display the map over a beige background
Actual Behaviour
Only displays the beige background container
Thanks for the report! The root cause of this is that after our switch from a ES5 bundle to a ES6 one, Babel transpilation of the bundle breaks it due to the way we set up worker code. We're investigating ways to address this, but the current workaround is to configure your app to exempt mapbox-gl from being transpiled.
Thanks for the explanation!
I'm trying to exclude it from the transpilation, I understand it's more a Babel issue but do you happen to know how I can exclude mapbox-gl in my current situation?
I tried:
vue.config.js
module.exports = {
css: { extract: false },
configureWebpack: {
performance: {
hints: false,
maxEntrypointSize: 512000,
maxAssetSize: 512000
},
plugins: []
},
chainWebpack: config => {
config.module
.rule('js')
.test(/\.js/)
.exclude
.add(/node_modules\/mapbox-gl/)
.end()
.use('cache-loader')
.loader('cache-loader')
.end()
.use('babel-loader')
.loader('babel-loader')
.end()
}
}
I just wanted to add that I get the same behavior with an Angular project which of course is also using webpack for transpilation. FWIW I'm also targeting ES6 and that doesn't make a difference.
I don't know if this helps as it is not the direct issue, but its something I found when I was wondering if the problem was my fault: https://www.javaprogramto.com/2019/12/4-solutions-uncaught-referenceerror-is-not-defined.html
The other issue I noticed in attempting the 2.0.0 version is that I got the error suggesting that AMD or CommonJS scripts cause bailout issues; which in Angular-speak means I had to add it to my allowedCommonJsDependencies entry in the angular.json file. However, that was NOT true for previous releases.
I'm wondering if there is a CommonJS or other dependency that needs to be loaded and/or checked for being loaded ahead of mapbox-gl functionality being called? Is anything filename dependent? That is when the code gets moved to a shared chunk file with a funky long alphanumeric name. is something looking for mapbox-gl.js and not loading it? These questions are based on a read of the link above.
I also ran across this question on stack overflow and the marked answer is interesting: https://stackoverflow.com/questions/34973442/how-to-stop-babel-from-transpiling-this-to-undefined-and-inserting-use-str Given what I said about previous versions not complaining about CommonJS, Might you be compiling what is put up to npm as a commonJS wrapped package without knowing about it based on some babel or other configuration issue on your end?
Admittedly I'm not knowledgeable enough on the setups of everything y'all are using or the consequences of the changing to ES5->ES6, except for what the previous link mentions about different babel versions which wrap scripts in CommJS wrappers.
Hopefully there is something helpful here. If you put up a potential fix I can update my project to use via npm I will be happy to try again and report any findings.
@mourner @arindam1993 I can't manage to make this work without the fix. The map part of my library will be on hold until it's done. Do you guys have an approximate ETA for this?
@antoinealej still investigating this as it's quite a tricky problem, but we expect to have a fix as a part of v2.0.1 in about a week or two, and will also try to find a definite workaround early next week.
I have the same issue after upgrading to 2.0 in an app created with "Create react app". It breaks when loading the map, but it works afterwards while dynamically changing the styles. Rolling back to v1 for the time being.
Same issue here
I have the same issue with React with Create React App. Works great in dev, but does not work with a production build.
Repros with as little as instantiating the map:
const newMap = new mapboxgl.Map({
container: "map",
style: 'mapbox://styles/mapbox-map-design/ckhqrf2tz0dt119ny6azh975y',
center: [-121, -34],
pitch: 50,
bearing: 80,
zoom: 10.0
});
with the following error:
[8c586751-6b29-401a-a42b-d3a9e4cf22ce:1 Uncaught ReferenceError: y is not defined
Same behavior observed in a gatsbyjs app, maps work fine when running gatsby develop, built site throws Uncaught ReferenceError: m is not defined
Heads up that so far we've landed #10219 to make it easier to fix (will be included in the v2.0.1 patch release), and are also adding a docs section on transpilation issues: https://github.com/mapbox/mapbox-gl-js-docs/pull/461
It looks like the fixes will require customizing our bundlers. Is there any fix planned that will help those of us using Create React App with no access to our bundler?
Console Error:-

Production Build Screenshot-

I pulled my hair for 3 hours to find the issue.
One workaround for Create React App, where the bundler config is not configurable, is to use Webpack's inline loader syntax.
First, install the worker-loader package (yarn add worker-loader). Then add the following snippet somewhere in your app, ideally right below your imports:
// eslint-disable-next-line import/no-webpack-loader-syntax
mapboxgl.workerClass = require('worker-loader!mapbox-gl/dist/mapbox-gl-csp-worker').default;
You probably need the eslint-disable line so Create React App doesn't error out the build.
Hello! Are there any workarounds for Create React App integration? Unfortunately replacing workerClass did not solve the issue. Thank you
Taking @LeVadim's comment a bit further after reflecting on it a couple of days - I think you all will have a lot of issues filed on you on a constant basis if this doesn't "just work" with create-react-app given its very high level of popularity.
One workaround for Create React App, where the bundler config is not configurable, is to use Webpack's inline loader syntax (after installing the
worker-loaderpackage):// eslint-disable-next-line import/no-webpack-loader-syntax mapboxgl.workerClass = require('worker-loader!mapbox-gl/dist/mapbox-gl-csp-worker').default;You probably need the
eslint-disableline so Create React App doesn't error out the build.
I tried this an get the same error.
One workaround for Create React App, where the bundler config is not configurable, is to use Webpack's inline loader syntax (after installing the
worker-loaderpackage):// eslint-disable-next-line import/no-webpack-loader-syntax mapboxgl.workerClass = require('worker-loader!mapbox-gl/dist/mapbox-gl-csp-worker').default;You probably need the
eslint-disableline so Create React App doesn't error out the build.I tried this an get the same error.
@momolarson this workaround works for me in a React app. I added the worker-loader package (yarn add worker-loader), added the two lines after the imports, and the result of yarn build works fine for me.
@rsippl - wow! thanks for the tip! I had added it before my imports (specifically AFTER the mapbox-gl import). I really appreciate that!
Just making it super concrete for future create-react-users that come across this, a non-eject production build solution is to import mapboxgl like this:
import 'mapbox-gl/dist/mapbox-gl.css';
import mapboxgl from 'mapbox-gl';
// @ts-ignore
// eslint-disable-next-line import/no-webpack-loader-syntax
mapboxgl.workerClass = require('worker-loader!mapbox-gl/dist/mapbox-gl-csp-worker').default;
@arindam1993 @mourner You might consider adding this to the transpilation documentation.
@timfpark yes, this is being added in https://github.com/mapbox/mapbox-gl-js-docs/pull/461