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

6.x.x production build error "x is not defined" #1266

Open
testower opened this issue Dec 18, 2020 · 49 comments
Open

6.x.x production build error "x is not defined" #1266

testower opened this issue Dec 18, 2020 · 49 comments
Labels

Comments

@testower
Copy link

testower commented Dec 18, 2020

I upgraded to 6.0.0 (also tried 6.0.1) and everything works fine in development mode, but in the production build the background map doesn't render using. I'm using a mapbox token.

        <StaticMap
          key="map"
          width="100%"
          height="100%"
          mapStyle={MAPBOX_MAP_STYLE}
          mapboxApiAccessToken={MAPBOX_ACCESS_TOKEN}
        />

I have an IconLayer that renders just fine, but on top of a grey background :/

@Pessimistress
Copy link
Collaborator

Do you see any error in the console? Might be caused by mapbox/mapbox-gl-js#10173

@testower
Copy link
Author

Thanks, it seems related, I will follow that issue!

@Velidoss
Copy link

In production build I've got this error. Could you help, please?
image

@Pessimistress
Copy link
Collaborator

Pessimistress commented Dec 24, 2020

@Velidoss please visit the Mapbox issue I linked above. The bug is in mapbox-gl@2.0.0. There is a temporary work around by adding additional bundler settings.

@sgup
Copy link

sgup commented Dec 24, 2020

Same issue here. As a quick workaround I downgraded to v5.2.11 of react-map-gl:

yarn upgrade react-map-gl@5.2.11 and it works.

@zanemountcastle
Copy link

Downgrading to v5.2.11 of react-map-gl was insufficient for my use case since I'm using Mapbox GL JS v2.x.x features (setTerrain() specifically)

For those out there in a similar spot, I was able to hack around things for the time being by adapting the suggestions in mapbox/mapbox-gl-js#10173:

Install worker-loader:

yarn add worker-loader

In your map component:

import ReactMapGL from "react-map-gl";
import mapboxgl from "mapbox-gl"; // This is a dependency of react-map-gl even if you didn't explicitly install it

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

I hope the Mapbox team can provide a long term solution soon.

@robinjhuang
Copy link

Having the same issue

@aditbharadwaj
Copy link

Having the same issue

use the above work around for mapbox worker class it works gr8 for now 👍

@marc-slingshot
Copy link

looks like they are working on a PR to address this here - https://github.com/mapbox/mapbox-gl-js-docs/pull/461

Looks like the "right" way might be to add this code:

import mapboxgl from 'mapbox-gl/dist/mapbox-gl';
import MapboxWorker from 'mapbox-gl/dist/mapbox-gl-csp-worker';
mapboxgl.workerClass = MapboxWorker;

@jiaranda
Copy link

Could someone explain why this works?

@Pessimistress Pessimistress pinned this issue Jan 15, 2021
tpliakas added a commit to tpliakas/MyCity-Social-Network that referenced this issue Jan 24, 2021
@Pessimistress Pessimistress changed the title After upgrading to 6.x.x background map does not render in production build 6.x.x production build error "x is not defined" Jan 24, 2021
@Pessimistress
Copy link
Collaborator

Mapbox has now published the official solution to this issue: https://docs.mapbox.com/mapbox-gl-js/api/#transpiling-v2

@JG145
Copy link

JG145 commented Feb 1, 2021

I am still struggling to get this to work. I am able to get Mapbox-GL to work by using:
import mapboxgl, { queryRenderedFeatures } from "!mapbox-gl";

but still getting:
Uncaught ReferenceError: _createClass is not defined

sure i am missing something obvious but any help would be appreciated

@kentongray
Copy link
Contributor

@JG145 what I had to do was the following (as a create react app and typescript user)

yarn add worker-loader

/* eslint-disable @typescript-eslint/no-var-requires */
(mapboxgl as any).workerClass = require('worker-loader!mapbox-gl/dist/mapbox-gl-csp-worker').default;

@JG145
Copy link

JG145 commented Feb 1, 2021

Still hitting the same issue?

gone with:

import mapboxgl from "!mapbox-gl";

import { StaticMap } from "!react-map-gl";
/* eslint-disable @typescript-eslint/no-var-requires */
mapboxgl.workerClass = require("worker-loader!mapbox-gl/dist/mapbox-gl-csp-worker").default;

Changing the above to:

import mapboxgl from "mapbox-gl";

import { StaticMap } from "react-map-gl";
/* eslint-disable @typescript-eslint/no-var-requires */
mapboxgl.workerClass = require("worker-loader!mapbox-gl/dist/mapbox-gl-csp-worker").default;

elicits this error:

VM1351:63121 Uncaught DOMException: Failed to construct 'Worker': Script at 'https://app.powerbi.com/13.0.15255.54/assetsmapbox-gl-csp-worker' cannot be accessed from origin 'null'.

@vehere-ccuMaksym
Copy link

Also get no clue how to solve this issue in versions above 5.3.4. Downgrade to v 5.3.4 solves it for now for me. If someone got a result on versions 6.0.0 and above for React, please give some code example here.

@Antje3
Copy link
Contributor

Antje3 commented Feb 11, 2021

Downgrading to v5.2.11 of react-map-gl was insufficient for my use case since I'm using Mapbox GL JS v2.x.x features (setTerrain() specifically)

For those out there in a similar spot, I was able to hack around things for the time being by adapting the suggestions in mapbox/mapbox-gl-js#10173:

Install worker-loader:

yarn add worker-loader

In your map component:

import ReactMapGL from "react-map-gl";
import mapboxgl from "mapbox-gl"; // This is a dependency of react-map-gl even if you didn't explicitly install it

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

I hope the Mapbox team can provide a long term solution soon.

If you use typescript, just add // @ts-ignore above the eslint-disable line to ignore the typescript error when compiling (Property 'workerClass' does not exist on type 'typeof mapboxgl'.). This worked for me.

aperkaz added a commit to aperkaz/taggr that referenced this issue Nov 16, 2021
aperkaz added a commit to aperkaz/taggr that referenced this issue Nov 16, 2021
* Add sdorsys boilerplate

* Add sharp w typescript

* Add frontend mock

* Refactor backend into /electron

* Add FE module

* Add dynamic loading of FE assets

* Add TS support to the FE

* Add basic yarn workspace (shared/frontend)

* Add waiting to electron module & update module import in electron

* Update readme

* Add storybook support

* Add eslint to the electron ts code

* Add jest support in the backend

* Add tests to FE

* Clean FE styles

* Add render to render IPC communications

* Add atoms and molecules (component library)

* Add molecules and organisms

* Add navBar molecule

* Update molecules

* Add start page, add static and clean tests

* Add pre-processing and settings pages

* Fix gallery bug and add dashboard page

* Add shared types and add component type inference

* Add redux dependency to FE

* Add redux and real types

* Update shared module types, message bug, and todos

* Add BE messageBus handler skeleton & ML

* Add DB module and tests

* Add destroy handler, type refactors and twitter link

* Add reset handler dep injection and tests

* Update handlers and add utils

* Break down services and add dependency injection

* Add tests for the date service

* Add file service types and tests

* Add test coverage and comments to image service

* Add exposed types in services and handlers

* Fix webId population, add handler dep injection

* Clean up sharp loading and add processing (not working yet)

* Add test skeleton for the ML operations and test-images

* Add tag tests, update classification services and types

* Add custom tag types

* Update tests

* Add filter images with location, update types and message bus

* Fix backend tests and clean up FE dead tests

* Update FE build leftovers

* Enable loading local image files

* Add FE fonts

* Add ML dependencies to BE

* Add box shadow to UI buttons

* Clean up isProcessing dependency from FE -> Redux

* Remove unused prop isProcessing from store and UI

* Fix flanky test (jest --clearCache)

* Add fallback to map component

* Fix shared build error (due to TF)

* Fix button shadow

* Silence process errors and fix image paths

* Add react and redux extensions to electron main

* Clean up unused config

* Fix filtering bug

* Add app icon and update todos

* Update app naming in FE

* Remove isDev dependency + skeleton of docs

* Add CI support

* Update CI workflow files

* Add craco to fix map bug 

visgl/react-map-gl#1266

* Clean up sharp dependency

* Fix the map issue by rolling back

* Hide backend window in prod

* Update isDev flag

* Clean tasks

* Add partial README

* Add documentation

* Optimize build

* Update tests

* Update CI to only run on PRs

* Clean up old README

* Remove CI

* Add screenshot
@stevepepple
Copy link

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

Thanks so much for this. I'd tried a few other solutions and wasn't sure what do try next!

@MalikOwais123
Copy link

mapbox-gl

Is it for production fixes also because i tried and it is working fine on development mode..

Can we use this version on prod as well..

@sangdth
Copy link

sangdth commented Feb 4, 2022

Update: There was a fix on Nextjs, but still in canary branch.


vercel/next.js#30237 (comment)

@ariel-upstream
Copy link

Is there any elegant solution for that (react-scripts)?

@mohammad6vakili
Copy link

downgrade to version @5.2.11 and it will be ok

@kyuumeitai
Copy link

Hi there, I'm writing from 2022 and downgrading did the trick for me.

@sloev
Copy link

sloev commented Mar 30, 2022

hi, i still recommend ejecting from create-react-app and specifying the webpack config like in my previous comment, it means i can upload as i find fit.
(#1266 (comment))

@tclaff
Copy link

tclaff commented Apr 11, 2022

Had the same problem using react-map-gl together with maplibre. I could manage to fix it by using craco (https://github.com/gsoft-inc/craco). Here is my craco.config.js:

const CracoWorkboxPlugin = require('craco-workbox');

module.exports = {
  babel: {
    loaderOptions: {
      // this option lets us display the map-pin marker layer - without this it does not work: https://github.com/visgl/react-map-gl/issues/1266
      ignore: ['./node_modules/maplibre-gl/dist/maplibre-gl.js']
    }
  },
  plugins: [{
    plugin: CracoWorkboxPlugin
  }],
  webpack: {
    alias: {
      'mapbox-gl': 'maplibre-gl',
    },

  },
};

Hope this helps someone.

@dorthwein
Copy link

dorthwein commented May 10, 2022

Speaking from 2022 as well - after trying many things, this is the comment to follow for a direct few line fix on a create react-app without having to eject

#1266 (comment)

@strimbob
Copy link

strimbob commented Oct 6, 2022

does this still not work, tried #1266 (comment) but i get an error with typescript and does not work, feels mega hacky -

update this did work just had to add the // @ts-ignore

// @ts-ignore
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; /* eslint import/no-webpack-loader-syntax: off */

thanks everyone for figuring it out

@emekaokoli
Copy link

December 2022 I am facing this issue in production only.

Throws this error
errro

 "Mapbox-gl": "^2.9.2",
 "react-map-gl": "^7.0.19",
 "react": "^18.2.0",
 "react-dom": "^18.2.0",

The map is blank
map error

can someone please help, it's killing me.

@emekaokoli
Copy link

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

throws Do not use import syntax to configure webpack loaders in react

@emekaokoli
Copy link

December 2022 I am facing this issue in production only.

Throws this error errro

 "Mapbox-gl": "^2.9.2",
 "react-map-gl": "^7.0.19",
 "react": "^18.2.0",
 "react-dom": "^18.2.0",

The map is blank map error

can someone please help, it's killing me.

can anyone please help?

@alex-kowalczyk
Copy link

@emekaokoli, I have similar issue with maplibregl after building production build.
Map does not display any vector layer (except hill-shading raster layer which displays OK).
I found out that changing browserlist in package.json from

[">0.2%", "not dead", "not op_mini all"]

to

["last 1 firefox version", "last 1 chrome version"]

solves the issue, but obviously it's not suitable for production.
I did not find a "golden mean" yet for browserslist, if anyone knows, please share.

@emekaokoli
Copy link

emekaokoli commented Dec 20, 2022

December 2022 I am facing this issue in production only.
Throws this error errro

 "Mapbox-gl": "^2.9.2",
 "react-map-gl": "^7.0.19",
 "react": "^18.2.0",
 "react-dom": "^18.2.0",

The map is blank map error
can someone please help, it's killing me.

can anyone please help?

solved: https://docs.mapbox.com/mapbox-gl-js/guides/install/#transpiling

@martinjuhasz
Copy link

martinjuhasz commented Apr 28, 2023

While i could fix the initial error by using the worker-loader solution provided and the map tiles now loading, i still get errors.

The Map and tiles are loading, but city names f.e. will not show up. It works locally, so only on a production build this fails.

Javascript console error from the mapbox worker file is Error: "a" is read-only.

Any ideas how to fix this?

Edit:

I've digged a little deeper. So while this works

// @ts-ignore
import mapboxgl from 'mapbox-gl/dist/mapbox-gl-csp'
// @ts-ignore
// eslint-disable-next-line import/no-webpack-loader-syntax, import/no-unresolved
import MapboxWorker from 'worker-loader!mapbox-gl/dist/mapbox-gl-csp-worker'
mapboxgl.workerClass = MapboxWorker

As soon as i use any mapboxgl related import it fails again. For example i'm doing some pan and zooming depending on boundaries and calculate them using LngLatBounds.

But when using the above technic it fails, both locally and in production

// @ts-ignore
import mapboxgl, { LngLatBounds } from 'mapbox-gl/dist/mapbox-gl-csp'
// @ts-ignore
// eslint-disable-next-line import/no-webpack-loader-syntax, import/no-unresolved
import MapboxWorker from 'worker-loader!mapbox-gl/dist/mapbox-gl-csp-worker'
mapboxgl.workerClass = MapboxWorker

While developing locally everything worked, using default imports.

import { LngLatBounds } from 'mapbox-gl'

// pseudo Code for reference

const fitMap = () => {
	// ...
	const bounds = new LngLatBounds(
		{
			lng: points[0][0],
			lat: points[0][1],
		},
		{
			lng: points[0][0],
			lat: points[0][1],
		},
	)
	for (const point of points) {
		bounds.extend([point[0], point[1]])
	}
	map.fitBounds(bounds, { padding: 30, maxZoom: 9 })
}

any ideas what causes this?

@erlajoua
Copy link

erlajoua commented Jul 5, 2023

downgrade to version @5.2.11 and it will be ok

INSANE WOKS ! THANKS , THIS MAN IS SO GOOD

@parsatajik
Copy link

Hey! I hosted my app on Netlify and was getting similar problems. The below steps fixed my issues:

  1. Exclude Mapbox GL JS from transpilation: You can exclude Mapbox GL JS from transpilation by adding an .env file in the root of your project with the following line:
    SKIP_PREFLIGHT_CHECK=true

  2. Target transpilation to ES6 with browserslist: You can target transpilation to ES6 by adding the following browserslist configuration to your package.json file:

"browserslist": {
  "production": [
    ">0.2%",
    "not dead",
    "not op_mini all",
    "not safari < 10",
    "not chrome < 51",
    "not android < 5",
    "not ie < 12"
  ],
  "development": [
    "last 1 chrome version",
    "last 1 firefox version",
    "last 1 safari version"
  ]
}

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

No branches or pull requests