3
votes

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

  1. $ gatsby new leaflet
  2. $ cd leaflet && npm install
  3. Install the package and dependencies$ npm install react-leaflet leaflet react react-dom
  4. Created src/pages/map.js
  5. 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="&amp;copy <a href=&quot;http://osm.org/copyright&quot;>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>
    )
  }
}
2

2 Answers

4
votes

I would use the modifyWebpackConfig code that you have. The minify error is because now that react-leaflet is an empty module, when it tries to build any page using react-leaflet components it will cause an error. You'll have to wrap your maps in a check to see if window is available. This way it will get skipped in server side rendering.

In React it would look something like this.

render() {
    if (typeof window !== 'undefined') {
        return (
            <Map {...options}>

            </Map>
        )
    }
    return null
}
2
votes

Just a heads up to future readers as per Oct 2019 Gatsby version; API have changed a little from the OPs version.

gatsby-node.js


exports.onCreateWebpackConfig = ({ stage, loaders, actions }) => {
  if (stage === 'build-html') {
    actions.setWebpackConfig({
      module: {
        rules: [
          {
            test: /react-leaflet|leaflet/,
            use: loaders.null(),
          },
        ],
      },
    })
  }
}