Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GatsbyJS & Superagent: WebpackError: require is not a function

I'm trying to build my react app into static html pages with gatsbyjs. I just tried 'gatsby build', and it goes through everything fine until it wants to create the static pages, then I get:

error Building static HTML for pages failed

See our docs page on debugging HTML builds for help


  1 | if (global.GENTLY) require = GENTLY.hijack(require);   2 |
> 3 | var crypto = require('crypto');
    | ^   4 | var fs = require('fs');   5 | var util = require('util'),   6 |     path = require('path'),


  WebpackError: require is not a function

  - incoming_form.js:3 Object.map../file
    ~/formidable/lib/incoming_form.js:3:1

  - index.js:1 Object.<anonymous>
    ~/formidable/lib/index.js:1:1

  - index.js:8 Object.<anonymous>
    ~/superagent/lib/node/index.js:8:1

  - contact.js:3 Object.<anonymous>
    src/pages/contact.js:3:1

  - sync-requires.js:8 Object.exports.__esModule
    .cache/sync-requires.js:8:53

I am using npm 5.5.1.

Edit:

So I just commented out superagent in my contact.js file, and the build goes through fine. However, I don't understand why this should cause any problems:

contact.js:

import request from 'superagent'

export default class Contact extends React.Component {

  constructor(props) {
    super(props);
    this.state = { showThankYou: false};

    this.handleSubmit = this.handleSubmit.bind(this);
  }

handleSubmit(e) {
    e.preventDefault();

    request.post('http://www.mywebsite.com/email.php') 
    .send(new FormData(document.getElementById('myForm')))
    .end(function(err, res){
      if (err || !res.ok) {
        console.log('Oh no! error' + err);
      } else {
        console.log('yay got it');
      }
    });

    document.getElementById("myForm").reset();
    this.setState({showThankYou: true});
  }


render() {  
    return (
    <div className="row">

        <div className="form_wrapper">
          <div>
            <form id="myForm" onSubmit={this.handleSubmit}> 

              <label htmlFor="fname">First Name</label>
              <input type="text" id="fname" name="fname" />

              <label htmlFor="lname">Last Name</label>
              <input type="text" id="lname" name="lname" />

              <label htmlFor="email">E-mail Address</label>
              <input type="text" id="email" name="email" />

              <label htmlFor="message">Message</label>
              <textarea id="message" name="message" style={{height: "100px"}}></textarea>

              <input type="submit" value="Submit" />

            </form>
          </div>
        </div>    
    </div>);
}
}
like image 493
George Welder Avatar asked Oct 25 '17 13:10

George Welder


People also ask

What is GatsbyJS used for?

According to their site, “Gatsby is a free and open source framework based on React that helps developers build blazing fast websites and apps”. Gatsby allows the developers to make a site using React and work with any data source (CMSs, Markdown, etc) of their choice.

Is GatsbyJS hard to learn?

Gatsby is based on React. js which is a JavaScript library for building User Interfaces using components. It's relatively easy to learn, and if you're able to write solid JavaScript code, you're good to go.

Why is GatsbyJS great?

Gatsby is built on top of React, and combines cutting edge technologies like GraphQL, Webpack, and more to create an incredibly elegant developer experience. Spend more time building your app and less time maintaining and optimizing it.

What is the difference between Gatsby and next JS?

Gatsby is for creating static websites with infrequent content changes, while NextJS is useful for building complicated sites with high server interaction. But even so, these two frameworks have significant differences between them that confuse developers as to which one to pick for robust web development.


1 Answers

I ran into this same error (coming from Auth0 in my case). I was able to resolve it by modifying the webpack config.

Gatsby v1

In gatsby-node.js add:

exports.modifyWebpackConfig = ({ config, stage }) => {
  config.plugin("webpack-define", webpack.DefinePlugin, [{ "global.GENTLY": false }])

  return config;
};

Gatsby v2

In gatsby-node.js add:

exports.onCreateWebpackConfig = ({ stage, actions, plugins }) => {
  actions.setWebpackConfig({
    plugins: [
      plugins.define({
        'global.GENTLY': false
      })
    ]
  })
}

I ended up running into other problems because auth0-lock doesn't support server side rendering, but this solved the require is not a function error.

like image 90
Sharon Kennedy Avatar answered Sep 28 '22 09:09

Sharon Kennedy