Coder Social home page Coder Social logo

Comments (13)

Giboork avatar Giboork commented on June 12, 2024 5

getting this error while using next.js

from react-map-gl-geocoder.

SamSamskies avatar SamSamskies commented on June 12, 2024 5

I finally got a chance to look into this more. I found a workaround that works and is recommended by Zeit. https://github.com/zeit/next.js/wiki/FAQ

// instead of doing this
// import Geocoder from 'react-map-gl-geocoder'

// do this
let Geocoder

if (typeof window !== 'undefined') { 
  Geocoder = require('react-map-gl-geocoder').default; 
}

At some point, I'll try and make a PR to the suggestions library to only set window.Suggestions if window is not undefined. If someone else gets around to doing that before me, could you please post a comment here to let everyone know. If we can get that PR merged, we'll also need to ask mapbox-gl-geocoder if they can update their suggestions dependency.

from react-map-gl-geocoder.

jaronheard avatar jaronheard commented on June 12, 2024 2

I ran into a similar issue as I was trying to get CIVIC's component library to work in SSR contexts, such as Gatsby.

Here's the similar error I saw running gatsby build, before using the null loader webpack config trick that Gatsby recommends, described in more detail in this PR

  55 |  */
  56 | var Suggestions = require('./src/suggestions');
> 57 | window.Suggestions = module.exports = Suggestions;
     | ^
  58 |


  WebpackError: ReferenceError: window is not defined

  - index.js:57 Object../node_modules/suggestions/index.js
    [lib]/[suggestions]/index.js:57:1

  - bootstrap:19 __webpack_require__
    lib/webpack/bootstrap:19:1

  - index.js:3 Object../node_modules/@mapbox/mapbox-gl-geocoder/lib/index.js
    [lib]/[@mapbox]/mapbox-gl-geocoder/lib/index.js:3:17

  - bootstrap:19 __webpack_require__
    lib/webpack/bootstrap:19:1

  - index.js:21 Object../node_modules/react-map-gl-geocoder/dist/index.js
    [lib]/[react-map-gl-geocoder]/dist/index.js:21:25

  - bootstrap:19 __webpack_require__
    lib/webpack/bootstrap:19:1

  - BaseMap.js:20 Object../node_modules/@hackoregon/component-library/dist/BaseMap/BaseMap.js
    [lib]/[@hackoregon]/component-library/dist/BaseMap/BaseMap.js:20:50

  - bootstrap:19 __webpack_require__
    lib/webpack/bootstrap:19:1

  - index.js:407 Object../node_modules/@hackoregon/component-library/dist/index.js
    [lib]/[@hackoregon]/component-library/dist/index.js:407:39

  - bootstrap:19 __webpack_require__
    lib/webpack/bootstrap:19:1


  - bootstrap:19 __webpack_require__
    lib/webpack/bootstrap:19:1

  - sync-requires.js:8 Object../.cache/sync-requires.js
    lib/.cache/sync-requires.js:8:53

  - bootstrap:19 __webpack_require__
    lib/webpack/bootstrap:19:1

  - static-entry.js:9 Module../.cache/static-entry.js
    lib/.cache/static-entry.js:9:22

  - bootstrap:19 __webpack_require__
    lib/webpack/bootstrap:19:1

I set up a sandbox to try to test if gatsby build errors on your example. It does error on gatsby build, but it's a different error. Not sure if it's helpful.

from react-map-gl-geocoder.

SamSamskies avatar SamSamskies commented on June 12, 2024

Hi @benbowler, could you provide some context as to how you're trying to use this module? For example, are you using Next.js, Gatsby, etc.? If you could provide a code sample using Code Sandbox that would be even better.

from react-map-gl-geocoder.

SamSamskies avatar SamSamskies commented on June 12, 2024

Just realized you typed pnpm and not npm. I'm actually not familiar with that package manager. Could you try loading just that suggestions package in your app using pnpm? If that doesn't work, I can file an issue with pnpm.

from react-map-gl-geocoder.

chiho13 avatar chiho13 commented on June 12, 2024

I'm also getting this error

from react-map-gl-geocoder.

SamSamskies avatar SamSamskies commented on June 12, 2024

Currently only npm and yarn are supported. It's currently not on my TODO list to support pnpm.

from react-map-gl-geocoder.

SamSamskies avatar SamSamskies commented on June 12, 2024

Hi @Giboork, thanks for reporting the issue. Looks like this may require a PR into the suggestions dependency to fix this. I don't think window.Suggestions should be set if there's no window https://github.com/tristen/suggestions/blob/gh-pages/index.js#L57.

In the meantime, I might be able to figure out a way to hack around this. Not sure if I'll have time this week though.

If you want to try and fix it on your end for now, you can check out this SO for some ideas. https://stackoverflow.com/questions/38951721/react-js-server-side-issue-window-not-found

from react-map-gl-geocoder.

CassiusHR avatar CassiusHR commented on June 12, 2024

I finally got a chance to look into this more. I found a workaround that works and is recommended by Zeit. https://github.com/zeit/next.js/wiki/FAQ

// instead of doing this
// import Geocoder from 'react-map-gl-geocoder'

// do this
let Geocoder

if (typeof window !== 'undefined') { 
  Geocoder = require('react-map-gl-geocoder').default; 
}

At some point, I'll try and make a PR to the suggestions library to only set window.Suggestions if window is not undefined. If someone else gets around to doing that before me, could you please post a comment here to let everyone know. If we can get that PR merged, we'll also need to ask mapbox-gl-geocoder if they can update their suggestions dependency.

This worked for me using Gatsby JS

from react-map-gl-geocoder.

milinpaul avatar milinpaul commented on June 12, 2024

For users who use next.js framework, importing mapbox-gl-geocoder dynamically will resolve the window is not defined issue.

use nextjs dynamic import from next/dynamic

from react-map-gl-geocoder.

SamSamskies avatar SamSamskies commented on June 12, 2024

@milinpaul thanks for the tip!

Also, I have a PR (tristen/suggestions#42) open with the suggestions library to fix this error. Feel free to +1 the PR 😀

from react-map-gl-geocoder.

SamSamskies avatar SamSamskies commented on June 12, 2024

My PR with the suggestions library has been merged! Now I need to file an issue with @mapbox/mapbox-gl-geocoder to get them to upgrade the dependency. Once they do that I can upgrade @mapbox/mapbox-gl-geocoder and close out this issue.

from react-map-gl-geocoder.

SamSamskies avatar SamSamskies commented on June 12, 2024

Made a PR with @mapbox/mapbox-gl-geocoder to upgrade the suggestions dependency mapbox/mapbox-gl-geocoder#403

from react-map-gl-geocoder.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.