question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Building fails because fetch is not defined

See original GitHub issue
gatsby-config.js
require(`dotenv`).config();

module.exports = {
  siteMetadata: {
    author: `Thibault Maekelbergh`,
    keywords: `thibault maekelbergh, thibmaek, blog`,
    title: `A nice blog about development, Raspberry Pi, plants and probably records`,
    social: {
      github: `thibmaek`,
      twitter: `thibmaek`,
    },
  },
  plugins: [
    `gatsby-plugin-react-helmet`,
    `gatsby-transformer-remark`,
    {
      resolve: `gatsby-source-contentful`,
      options: {
        accessToken: process.env.CONTENTFUL_TOKEN,
        spaceId: process.env.CONTENTFUL_SPACEID,
      },
    },
  ],
};
gatsby-node.js
const path = require(`path`);

const postTemplate = path.resolve(`./src/templates/post.jsx`);

exports.createPages = ({ graphql, boundActionCreators }) => {
  const { createPage } = boundActionCreators;

  return new Promise(async (resolve, reject) => {
    try {
      const posts = await graphql(`
        {
          allContentfulPost(limit: 1000) {
            edges {
              node {
                id
                slug
              }
            }
          }
        }
      `);

      if (posts.errors) reject(posts.errors);

      posts.data.allContentfulPost.edges.forEach(edge => {
        createPage({
          component: postTemplate,
          context: {
            id: edge.node.id,
            slug: edge.node.slug,
          },
          path: `/post/${edge.node.slug}/`,
        });
      });

      resolve();
    } catch (e) {
      console.error(e);
      reject(e);
    }
  });
};

Trying to run gatsby build fails because it can not find fetch:

error Building static HTML for pages failed

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

  12 | 
  13 |   componentWillMount() {
> 14 |     fetch(process.env.GATSBY_RECENT_REPOS_URL)
     |     ^
  15 |       .then(r => r.json())
  16 |       .then(repos => {
  17 |         this.setState({ repos: repos.reverse() });


  WebpackError: fetch is not defined
  
  - index.jsx:14 RecentRepos.componentWillMount
    src/components/recent-repos/index.jsx:14:5
  
  - ReactCompositeComponent.js:348 ReactCompositeComponentWrapper.performInitialMount
    ~/react-dom/lib/ReactCompositeComponent.js:348:1
  
  - ReactCompositeComponent.js:255 ReactCompositeComponentWrapper.mountComponent
    ~/react-dom/lib/ReactCompositeComponent.js:255:1
  
  - ReactReconciler.js:43 Object.mountComponent
    ~/react-dom/lib/ReactReconciler.js:43:1

I tried polyfilling this with unfetch, node-fetch, isomorphic-fetch, whatwg-fetch and none of these worked. I think it might be an issue, if not where do I start to fix this?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

6reactions
ciffeliacommented, Feb 11, 2018

If you don’t need polyfill for IE, you may use this code without isomorphic-fetch

typeof window !== 'undefined' && window.fetch(...)
  .then(...)
  .catch(...)
2reactions
KyleAMathewscommented, Nov 24, 2017

Try importing fetch instead of doing a polyfill.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReferenceError: fetch is not defined - javascript - Stack Overflow
If you use the new version(3.0.0) , it will get an error in import and then you'll get another one that says "fetch...
Read more >
ReferenceError: fetch is not defined in NodeJs | bobbyhadz
The "ReferenceError: fetch is not defined" occurs when the fetch() method is used in an environment where it's not supported - most commonly...
Read more >
Fetch is not defined error fixed nodejs ... - YouTube
In this video ,I try to fix an error ReferenceError: fetch is not defined in node js .Fetch can not use to make...
Read more >
ReferenceError: "x" is not defined - JavaScript - MDN Web Docs
The JavaScript exception "variable is not defined" occurs when there is a non-existent variable referenced somewhere.
Read more >
Troubleshooting Common Errors - Gatsby
Error : ReferenceError: window is not defined when running gatsby build · Build problems from Field 'browser' doesn't contain a valid alias configuration...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found