【问题标题】:TypeError: Cannot read property 'prototype' of undefined React ExpressTypeError:无法读取未定义 React Express 的属性“原型”
【发布时间】:2020-05-11 04:42:10
【问题描述】:

我有一个项目使用 Express 和 react,一切正常,

我没有对代码进行任何更改,它突然开始出现此错误。

前几天我把node从10.15.0升级到了12.x,加了axios。

我在这些更改之后进行了测试,它可以工作。现在不行了,这是什么意思??


TypeError: Cannot read property 'prototype' of undefined
(anonymous function)
C:/Desktop/ahmad/client/node_modules/express/lib/response.js:42
  39 |  * @public
  40 |  */
  41 | 
> 42 | var res = Object.create(http.ServerResponse.prototype)
  43 | 
  44 | /**
  45 |  * Module exports.
*/

View compiled
./node_modules/express/lib/response.js
http://localhost:3000/static/js/bundle.js:28181:30
__webpack_require__
C:/.../ahmad/client/webpack/bootstrap 4501180ca3adc0d915c0:678
  675 | };
  676 | 
  677 | // Execute the module function
> 678 | modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId));
  679 | 
  680 | // Flag the module as loaded
  681 | module.l = true;
View compiled
fn
C:/.../ahmad/client/webpack/bootstrap 4501180ca3adc0d915c0:88
  85 |      console.warn("[HMR] unexpected require(" + request + ") from disposed module " + moduleId);
  86 |      hotCurrentParents = [];
  87 |  }
> 88 |  return __webpack_require__(request);
  89 | };
  90 | var ObjectFactory = function ObjectFactory(name) {
  91 |  return {
View compiled
./node_modules/express/lib/express.js
C:/.../Desktop/ahmad/client/node_modules/express/lib/express.js:22
  19 | var Route = require('./router/route');
  20 | var Router = require('./router');
  21 | var req = require('./request');
> 22 | var res = require('./response');
  23 | 
  24 | /**
  25 |  * Expose `createApplication()`.
*/
View compiled
__webpack_require__
C:/.../Desktop/ahmad/client/webpack/bootstrap 4501180ca3adc0d915c0:678
  675 | };
  676 | 
  677 | // Execute the module function
> 678 | modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId));
  679 | 
  680 | // Flag the module as loaded
  681 | module.l = true;
View compiled
fn
C:/.../Desktop/ahmad/client/webpack/bootstrap 4501180ca3adc0d915c0:88
  85 |      console.warn("[HMR] unexpected require(" + request + ") from disposed module " + moduleId);
  86 |      hotCurrentParents = [];
  87 |  }
> 88 |  return __webpack_require__(request);
  89 | };
  90 | var ObjectFactory = function ObjectFactory(name) {
  91 |  return {
View compiled

【问题讨论】:

    标签: node.js reactjs webpack axios


    【解决方案1】:

    我发现我打错了

    import response from { 'express' }

    我发现当您尝试在前端调用后端内容时会发生此问题。 当我删除该行时问题解决了。

    编辑:原来我没有输入import语句,因为我使用了response这个词作为变量名,它是自动导入的。 我不确定导入是来自 React 本身还是另一个 VS 代码自动完成扩展。

    【讨论】:

    • 我被这个错误从何而来惊呆了,我做了同样的事情哈哈。自动导入可能很危险
    • 感谢这也发生在我身上。当我在 fetch 调用中输入“response”时,这是一个自动导入
    • 谢谢,伙计,我收到了这个错误,并试图从过去两天解决。
    【解决方案2】:

    从 { 'express' } 导入响应; 被进口了 默认情况下通过“vs code”在您的操作页面中

    【讨论】:

      【解决方案3】:

      我在处理一个文件时,突然出现了这个错误。然后我检查了第一行,这条import e from 'express' 行出现了,不知道为什么。我刚刚删除了它,它可以工作。

      【讨论】:

      • 完全正确。我确定我输入的是e.target.value,机器人认为它们会有所帮助。
      【解决方案4】:
      import { json } from "express" 
      

      这可能是由 vs 代码或反应自动导入的,所以只需删除它。

      【讨论】:

        【解决方案5】:

        太棒了!同样的问题,我不使用快递。当我创建一个 const 请求并使用智能来完成句子时,它发生在我身上。我删除了这条线

         import { request } from 'express';
        

        这是自动添加的,并且项目已经在运行

        【讨论】:

          【解决方案6】:

          或者你可以替换 express/lib/response.js 中的“res” (用任何其他变量替换“res”,“responsee”对我有用) (确保你也替换了导出语句中的“res”)

          【讨论】:

            【解决方案7】:

            这也发生在我身上,因为在 React 组件中使用handleSubmit 时,我忘记在参数中添加e(事件)并在函数内部调用e.preventDefault(),这个e 自动导入了上面提到的内容

            Here is the fix screenshot

            【讨论】:

            • 你能把代码作为答案的一部分(而不是图片)吗?
            【解决方案8】:

            我不小心在同一个文件夹中安装了 express 和 react,因此它无法正常工作。我为客户端/服务器创建了单独的文件夹,然后问题得到了解决

            【讨论】:

            • 这并不能真正回答问题。如果您有其他问题,可以点击 提问。要在此问题有新答案时收到通知,您可以follow this question。一旦你有足够的reputation,你也可以add a bounty 来引起更多的关注。
            猜你喜欢
            • 2021-10-31
            • 1970-01-01
            • 1970-01-01
            • 2021-08-01
            • 1970-01-01
            • 1970-01-01
            • 2017-11-03
            • 1970-01-01
            • 2019-05-05
            相关资源
            最近更新 更多