-
-
Notifications
You must be signed in to change notification settings - Fork 729
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
Uncaught ReferenceError: m is not defined - Create React App - browserslist - Internet Explorer IE #1011
Comments
No clue, try reproducing this in stackbliz. |
I tried stackblitz, but it doesn't seem to be able to handle a 'build' command in order to test the production environment. Anyway, I created a trivial new project, just a clean create-react-app application with a simple map:
And the result is the same: Works in development, but as soon as it's compiled using |
I'm not familiar enough with react to understand the differences between dev and prod build. |
IMHO you should use const Map: React.FC = () => {
const mapContainer = useRef<HTMLDivElement | null>(null);
useEffect(() => {
if (!mapContainer.current) return;
const map = new mapboxgl.Map({
container: mapContainer.current,
style: 'https://api.maptiler.com/maps/bright/style.json?key=******************',
});
}, []);
return <div style={{ height: 800 }} ref={mapContainer} />;
};
export default Map; Also, I think this wouldn't be a problem on |
Thanks, you're right that it's a cleaner approach, but tried it and it doesn't have any effect, the problem remains the same.
I'm not an expert in this field either, but afaik we are talking mostly just about webpack applied to the project. That's what I suppose many projects do and that's why I'm surprised that I'm the only one reporting this issue.
Do you mean any particular one? I'd give it a try, but to be honest, I don't see a reason why I'd get any different result after I apply webpack than I get in the sample project above. (not mentioning that I'm not sure I'd be able to use another library due to the ways I use the map - but that's beyond the scope of this issue and the sample project) |
The following is what I know is the goto when it comes to React: |
I tried a simple project and I got the very same problem. For the reference, it's like this:
I tried to find out more. The development webpack build apparently works fine, but the production build is where it breaks. I tried to pinpoint the issue, used webpack without minification and the error message is now
and after some search I found that it's from the ajax.ts:
Any idea? |
I'm having the same issue. My guess it's something to do with how Create React App is doing its bundling/minifying. But I don't understand enough of how that process works |
Digging a bit deeper in to this, it is related to what browser/js features are being targeted at (not sure the exact feature that causes the issue) This is why development builds work, but productions builds don't. Buy default CRA has a very wide browsers support for production but very restricted for development For example if you change your browserslist to something like the following, this will make your production build work (at least for the latest version of chrome) Not sure what the best solution here is? Would be good to know the exact feature that causes the change in the transpiling (ie what browsers we are unable to target) |
Thanks for the hint about broswerslist. If I add internet explorer to the development section in my package.json, I get the ReferenceError also in development mode: "browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version",
+ "ie 11"
]
} |
Note that MapLibre GL JS v2 dropped IE support altogether. |
I ended up using the following {
"name": "maplibre-v2",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.2",
"@testing-library/react": "^12.1.4",
"@testing-library/user-event": "^13.5.0",
"maplibre-gl": "^2.1.7",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "5.0.0",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
- ">0.2%",
- "not dead",
- "not op_mini all"
+ "defaults",
+ "not ie 11"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Note that I use |
Thanks for the updates, I can confirm that the change in targeted browsers really resolves the issue. In my case though, it was necessary to also include A note to anybody testing this: You might need to delete That said, the underlaying cause of the issue is still unclear. |
I can confirm, that adding |
Hi there, I am getting a similar error
Module parse failed: 'import' and 'export' may appear only with 'sourceType: module' (1:0)
File was processed with these loaders:
* ./node_modules/worker-loader/dist/cjs.js
* ./node_modules/babel-loader/lib/index.js
* ./node_modules/source-map-loader/dist/cjs.js
You may need an additional loader to handle the result of these loaders.
> export default function Worker_fn() {
| return new Worker(__webpack_public_path__ + "static/js/maplibre-gl-csp-worker.10c64516.worker.js");
| } I'm not sure if this is related to this issue (it looks like it overlaps?). |
Actually after fiddling a bit and crossing info from docs and github issues, I eventually got it to work in both dev and prod env. Here is what worked in case it helps anyone else:
"production": [
">0.2%",
"not dead",
"not op_mini all"
"not op_mini all",
"defaults", # line added as mentionned above in this issue
"not ie 11" # line added as mentionned above in this issue
], in // eslint-disable-next-line import/no-webpack-loader-syntax
import maplibregl from '!maplibre-gl'; // ! is important here
import maplibreglWorker from 'maplibre-gl/dist/maplibre-gl-csp-worker';
maplibregl.workerClass = maplibreglWorker; |
Just to clarify, I guess that the |
On my end it is still needed: I just tried again.
(I am using |
weblibregl doesn't have workerClass attribute. I'm using |
Fyi, now I also need the |
I'm using Gatsby 3.x. Adding the import { Map } from "maplibre-gl"; to //@ts-ignore
import maplibregl from "!maplibre-gl";
//@ts-ignore
import maplibreglWorker from "maplibre-gl/dist/maplibre-gl-csp-worker";
//@ts-ignore
maplibregl.workerClass = maplibreglWorker;
//Since I'm using typescript I have to import this one like this to be able to type the map variable
import { Map } from "maplibre-gl"; then you must initialize the map using: const map: Map = new maplibregl.Map({ ... }) Thanks everyone ! |
@apresmoi solutions worked for me too, using Gatsby 4. Thanks a lot ! |
Thx a lot @apresmoi and @theweaklink |
This seems to have been solved. Closing. |
I don't think that this issue should be closed yet. Not being able to be successfully compiled by Webpack is imho still an issue that we should try to find a real solution to. It's good that we have a workaround now, but it isn't a solution. |
I'm able to compile maplibre with angular that uses webpack. |
- Create POI and company layer components - Add all icons to map - Fetch POI data from geojson file - Fix bug maplibregl, react bug in production - [bug issue and solution](maplibre/maplibre-gl-js#1011 (comment))
Was getting the following error using current versions: Uncaught ReferenceError: g is not defined on maps when deployed The suggestion offered by @wipfli worked for me. Switched to a .browserslistrc containing: ` # NOTE: Was getting an error: Uncaught ReferenceError: g is not defined on maps when deployed [production] [development] I doubt switching to .browserslistrc made a difference but I took the opportunity to get this config out of my package.json |
Just in case anyone else runs into this issue, it is related to this upstream bug browserslist/browserslist#374, and the minimal fix is to add |
Hi, I'm trying to replace mapbox-gl and while it works fine in the developer mode, it breaks down when compiled into production using standard react-scripts.
maplibre-gl-js version: 2.1.6
I just use it like:
The map is properly drawn, but when compiled, I get error message:
from the workers and nothing is drawn. I'm not sure it's a bug, I suppose I might be doing something wrong, but don't see anything obvious, as the same approach works with mapbox-gl. Thanks for any idea!
The text was updated successfully, but these errors were encountered: