gatsby: WebpackError: self is not defined

Description

Suddently I cannot build anymore while develop is working well. I have the following error :

error Building static HTML for pages failed

See our docs page on debugging HTML builds for help https://goo.gl/yL9lND

   6 |
   7 |   var support = {
>  8 |     searchParams: 'URLSearchParams' in self,
     | ^
   9 |     iterable: 'Symbol' in self && 'iterator' in Symbol,
  10 |     blob:
  11 |       'FileReader' in self &&


  WebpackError: self is not defined

  - fetch.umd.js:8
    ~/whatwg-fetch/dist/fetch.umd.js:8:1

  - fetch.umd.js:2 support.searchParams
    ~/whatwg-fetch/dist/fetch.umd.js:2:1

  - fetch.umd.js:5 Object.defineProperty.value
    ~/whatwg-fetch/dist/fetch.umd.js:5:2

  - index.cjs.js:3 Object.<anonymous>
    ~/@firebase/polyfill/dist/index.cjs.js:3:1

  - index.cjs.js:5 Object.<anonymous>
    ~/firebase/app/dist/index.cjs.js:5:1

  - firebase.js:1 Object.<anonymous>
    src/firebase/firebase.js:1:1

Steps to reproduce

Just run gatsby build

Expected result

Get files in public folder.

Actual result

Crash on build. The real fun is that gatsby build used to work and I did tag a specific commit to make sure I can go back to this “working version”. I revert to this commit and surprise, it’s not working anymore.

Environment

gatsby info --clipboard

  System:
    OS: macOS High Sierra 10.13.6
    CPU: x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 8.12.0 - /usr/local/bin/node
    Yarn: 1.7.0 - /usr/local/bin/yarn
    npm: 6.4.1 - /usr/local/bin/npm
  Browsers:
    Chrome: 69.0.3497.100
    Firefox: 61.0.1
    Safari: 12.0
  npmPackages:
    gatsby: 1.9.266 => 1.9.266
    gatsby-link: 1.6.44 => 1.6.44
    gatsby-plugin-react-helmet: 2.0.11 => 2.0.11
    gatsby-plugin-react-next: 1.0.11 => 1.0.11
    gatsby-plugin-sass: 1.0.26 => 1.0.26
    gatsby-plugin-typography: 1.7.19 => 1.7.19
  npmGlobalPackages:
    gatsby-cli: 2.4.2

package.json

    "axios": "0.18.0",
    "bootstrap": "4.1.1",
    "firebase": "5.0.4",
    "gatsby": "1.9.266",
    "gatsby-link": "1.6.44",
    "gatsby-plugin-react-helmet": "2.0.11",
    "gatsby-plugin-react-next": "1.0.11",
    "gatsby-plugin-sass": "1.0.26",
    "gatsby-plugin-typography": "1.7.19",
    "react-helmet": "5.2.0",
    "serve": "^10.0.2",
    "typography-theme-ocean-beach": "0.15.11"

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 4
  • Comments: 34 (8 by maintainers)

Commits related to this issue

Most upvoted comments

Hi there, turns out you can use a null loader to allow it to build properly. Worked for me with firebase, at least:

In gatsby-node:

exports.onCreateWebpackConfig = ({actions, stage}) => {
    if (stage === "build-html") {
        actions.setWebpackConfig({
            module: {
                rules: [
                    {
                        test: /firebase/,
                        use: ['null-loader']
                    },
                ],
            }
        })
    }
};

Awesome! Thank you! I have also found another solve for this issue,

  1. Delete node_modules
  2. run npm i whatwg-fetch@2.0.4 --save
  3. run npm install

importing it into the gatsby-browser.js file fixed it for me.

I came to this thread due to whatwg-fetch causing me similar issues (however I am not using Firebase). With one version, it would work in build but not develop. Downgrading from 3.0.0 to 2.0.4 caused the opposite, failing build and working in develop. Details in my comment in a similar issue: https://github.com/gatsbyjs/gatsby/issues/3006#issuecomment-459540756.

Summary: I switched to fetch-ponyfill and it worked in both scenarios.

Sure, use gatsby-plugin-layout. And the key implementation is in https://github.com/muhajirframe/gatsby-starter-firebase/blob/master/src/layouts/index.js .

Basicly it load firebase on componentDidMount(). And put firebase in react context that’s it.

Same issue with whatwg-fetch, 2.0.4 → 3.0.0.

@adberard the issue was caused by ‘window’ not being defined in a gatsby build. I had imported ‘whatwg-fetch’ in a javascript file like normal but it needs adding to the gatsby-browser.js file here.

import ‘whatwg-fetch’

I’m assuming firebase has the same issue where it’s using fetch internally and using the library in a gatsby project is causing the same issue.

FWIW to future visitors of this thread, in response to my previous post, gatsby-plugin-layout and putting a firebase instance into context are not necessary steps to solving the issue of loading the Firebase SDK with Gatsby. Dynamic importing is necessary. Here’s why.

Firebase has an initializeApp method that makes reference to the window object. Gatsby does not have access to the window object in its build phase. Thus, we need to delay the initialization of Firebase until we have access to that object. Dynamically importing it in a component’s componentDidMount lifecycle hook guarantees this. However, this is not the end of solving the problem.

The reason context was used, but isn’t the only means of solving this problem, is to create a Firebase singleton in your application, that is, only one instance of initialized Firebase being consumed by your app. If you’re going to dynamically import and then initialize Firebase in the client, you will have to come up with a strategy to guarantee you only initialize one instance and use it through out your app. The example above by @muhajirframe does show an example with the getFirebase function.

Best of luck getting this working. I wrote about it in a blog post should you want more details of how I solved this problem: https://kyleshevlin.com/firebase-and-gatsby-together-at-last.