Comments (13)
getting this error while using next.js
from react-map-gl-geocoder.
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.
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.
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.
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.
I'm also getting this error
from react-map-gl-geocoder.
Currently only npm
and yarn
are supported. It's currently not on my TODO list to support pnpm
.
from react-map-gl-geocoder.
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.
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 setwindow.Suggestions
ifwindow
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 askmapbox-gl-geocoder
if they can update theirsuggestions
dependency.
This worked for me using Gatsby JS
from react-map-gl-geocoder.
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.
@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.
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.
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)
- GeoCoder Marker's Styling Not Working HOT 2
- Search Results Not closing, and marker not dropping HOT 9
- Geocoder doesn't appear in Next.js app HOT 1
- Console error passing object to marker prop
- Suggestions are displayed after setting a default input HOT 3
- External geocoder HOT 1
- React 17? HOT 1
- how to active draggable after search HOT 1
- mapbox-gl neccessary dependency? HOT 1
- How to get the current value? HOT 3
- Typescript import issue HOT 1
- Supporting stand-alone Geocoder without map ? HOT 1
- react-map-gl 7 support HOT 7
- Multiple input options in react app HOT 1
- export 'FlyToInterpolator' (imported as 'r') was not found in 'react-map-gl' HOT 2
- Request to upgrade transitive dependency `http-cache-semantics` in `react-map-gl-geocoder` HOT 1
- Menu stays open HOT 2
- Upon passing Marker="true" into geocoder, no point appears HOT 4
- TypeError: Cannot read property 'remove' of undefined __ Cannot read property 'addControl' of null - HOT 6
- Can Not Use react.map.gl.gecoder with Typescript HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-map-gl-geocoder.