Unexpected '!' in 'worker-loader!mapbox-gl/dist/mapbox-gl-csp-worker'
Asked Answered
B

1

13

hi I'm trying to use mapbox-gl in React with this tutorial. and I'm getting this error : Unexpected '!' in 'worker-loader!mapbox-gl/dist/mapbox-gl-csp-worker' while importing "worker-loader!mapbox-gl/dist/mapbox-gl-csp-worker"

here is my important codes:

Map.js

import React from 'react'
import mapboxgl from 'mapbox-gl/dist/mapbox-gl-csp';
import MapboxWorker from 'worker-loader!mapbox-gl/dist/mapbox-gl-csp-worker';

mapboxgl.workerClass = MapboxWorker;
mapboxgl.accessToken = " the token ";

export default class Map extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            lng: -70.9,
            lat: 42.35,
            zoom: 9
        };
        this.mapContainer = React.createRef();
    }
    componentDidMount() {
        const { lng, lat, zoom } = this.state;
        const map = new mapboxgl.Map({
            container: this.mapContainer.current,
            style: 'mapbox://styles/mapbox/streets-v11',
            center: [lng, lat],
            zoom: zoom
        });
    }

    render() {
        const style = {
            "position": "absolute",
            "top": "0",
            "right": "0",
            "left": "0",
            "bottom": "0"
        }
        const { lng, lat, zoom } = this.state;
        return (
            <div>
                <div ref={this.mapContainer} className={style} />
            </div>
        );
    }
}

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import reportWebVitals from './reportWebVitals';
import Map from "./Map";

ReactDOM.render(
    <Map/>,
  document.getElementById('root')
);

reportWebVitals();

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
    <link href='https://api.mapbox.com/mapbox-gl-js/v2.1.1/mapbox-gl.css' rel='stylesheet' />
    
    <title>React App</title>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
  </body>
</html>

package.json

{
  "name": "mapboxtest",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.9",
    "@testing-library/react": "^11.2.5",
    "@testing-library/user-event": "^12.8.1",
    "mapbox-gl": "^2.1.1",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-scripts": "4.0.3",
    "web-vitals": "^1.1.0",
    "worker-loader": "^3.0.7"
  },
  "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"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

can anyone help me to solve this problem?

Buffalo answered 4/3, 2021 at 14:22 Comment(2)
Yeah, you have an unexpected ! in 'worker-loader!mapbox-gl/dist/mapbox-gl-csp-worker'Bergmann
@RobertoZvjerković What should i replace it with? ... I just followed the tutorial..Buffalo
F
16

Do this:

// eslint-disable-next-line import/no-webpack-loader-syntax
import MapboxWorker from 'worker-loader!mapbox-gl/dist/mapbox-gl-csp-worker';

For More Detail:

https://mcmap.net/q/898124/-webpack-2-expected-39-39-error-using-with-import

https://medium.com/@daveberning/if-anyone-is-running-into-some-linter-errors-regarding-inline-import-statements-add-this-comment-3369e5e6779

https://lifesaver.codes/answer/es-lint-rule-import-no-webpack-loader-syntax-please-deactivate

For TS Project: How to import a file into a react app that uses create react app as raw text?

Futtock answered 4/3, 2021 at 18:15 Comment(1)
None of these worked for me, for some reason the issue is very persistent. I don't know what to doGstring

© 2022 - 2024 — McMap. All rights reserved.