I am having troubles trying to fix that React-Leaflet
/ Leaflet
expects window to be defined and causes the Gatsby build npm run build
to fail.
Error message:
WebpackError: window is not defined
- leaflet-src.js:230
~/leaflet/dist/leaflet-src.js:230:1
- leaflet-src.js:7 version
~/leaflet/dist/leaflet-src.js:7:1
- leaflet-src.js:10 Object.exports.__esModule
~/leaflet/dist/leaflet-src.js:10:2
- AttributionControl.js:5 Object.<anonymous>
~/react-leaflet/lib/AttributionControl.js:5:1
- index.js:26 Object.exports.__esModule
~/react-leaflet/lib/index.js:26:1
- map.js:2 Object.exports.__esModule
src/pages/map.js:2:1
The Gatsby docs lists a few possible scenarios or solutions. https://www.gatsbyjs.org/docs/debugging-html-builds/#fixing-third-party-modules
So by trying to follow the examples listed, I have tried adding in the gatsby-node.js
gatsby-node.js
exports.modifyWebpackConfig = ({ config, stage }) => {
if (stage === 'build-html') {
config.loader('null', {
test: /react-leaflet/,
loader: 'null-loader',
})
}
}
Both test: /leaflet/
and test: /react-leaflet/
But that spits out a WebpackError: Minified React error #130
I would really appreciate any help in getting rid of these build errors.
Thanks very much.
The steps and the code
$ gatsby new leaflet
$ cd leaflet && npm install
- Install the package and dependencies
$ npm install react-leaflet leaflet react react-dom
- Created
src/pages/map.js
- Copy / paste the simple example from https://github.com/PaulLeCam/react-leaflet/blob/master/example/components/simple.js
map.js
import React, { Component } from 'react'
import { Map, TileLayer, Marker, Popup } from 'react-leaflet'
export default class SimpleExample extends Component {
state = {
lat: 51.505,
lng: -0.09,
zoom: 13,
}
render() {
const position = [this.state.lat, this.state.lng]
return (
<Map center={position} zoom={this.state.zoom}>
<TileLayer
attribution="&copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors"
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<Marker position={position}>
<Popup>
A pretty CSS3 popup. <br /> Easily customizable.
</Popup>
</Marker>
</Map>
)
}
}