【问题标题】:TypeError: Cannot read properties of undefined (reading 'prototype') React Typescript + ExpressTypeError:无法读取未定义的属性(读取“原型”)React Typescript + Express
【发布时间】:2021-10-31 12:13:52
【问题描述】:

我正在尝试在我的 React-Typescript SPA 中使用 express。但它给了我错误:

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

我尝试将target: "node" 添加到我的"node_modules/react-scripts/config/webpack.config.js" 但它不起作用...

以下是我已经尝试解决的一些方法:

    import express from "express";
    app = express();
---------------
    import express from "express";
    let app: express.Application;
    app = express();
-------------

    import express from "express";
    let app: any;
    app = express();

-------------
    const express require("express");
    let app: express.Application;
    app = express();

【问题讨论】:

  • 我认为问题在于您忘记使用 const、var 或 let 实例化 app,因为它应该可以工作。
  • @KennethLew 抱歉我复制不正确,但我使用的是 const app = express()
  • @KennethLew 我看到了那个帖子,但它对我不起作用......
  • 或许可以试试import * as express from "express"

标签: javascript reactjs typescript express


【解决方案1】:

你必须同时实例化 app 和 express,同时使用 const 关键字。

const express = require('express');
const app = express();

另外,在你的代码库中寻找你拥有的任何地方:

import { response } from 'express';

这通常是错误输入的,删除该行可能会解决错误

【讨论】:

  • 非常感谢。就是这个东西:“import { response } from 'express”。花了一个小时寻找问题,你为我节省了很多时间)
【解决方案2】:

遇到同样的问题

它是在

下导入的
import e from "express";

不确定是否有任何随机 IntelliSense 扩展或 GH copilot 自动完成

【讨论】:

    猜你喜欢
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    • 2021-07-09
    • 2021-08-01
    • 2018-03-13
    • 2023-02-03
    • 1970-01-01
    相关资源
    最近更新 更多