【问题标题】:GatsbyJS & Superagent: WebpackError: require is not a functionGatsbyJS & Superagent: WebpackError: require is not a function
【发布时间】:2018-04-06 15:49:36
【问题描述】:

我正在尝试使用 gatsbyjs 将我的 react 应用程序构建到静态 html 页面中。我刚刚尝试了“gatsby build”,它一切正常,直到它想要创建静态页面,然后我得到:

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

我正在使用 npm 5.5.1。

编辑:

所以我只是在我的contact.js 文件中注释掉了superagent,并且构建顺利进行。但是,我不明白为什么这会导致任何问题:

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>);
}
}

【问题讨论】:

    标签: javascript reactjs webpack superagent gatsby


    【解决方案1】:

    我遇到了同样的错误(在我的例子中来自 Auth0)。我能够通过修改 webpack 配置来解决它。

    盖茨比 v1

    在gatsby-node.js 中添加:

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

    盖茨比 v2

    在gatsby-node.js 中添加:

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

    我最终遇到了其他问题,因为 auth0-lock 不支持服务器端渲染,但这解决了 require is not a function 错误。

    【讨论】:

    • 你还需要const webpack = require('webpack')才能得到webpack.DefinePlugin
    猜你喜欢
    • 1970-01-01
    • 2018-10-25
    • 2016-01-05
    • 2020-08-03
    • 2017-06-22
    • 2019-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多