TypeError:无法读取未定义的属性(读取“原型”)React Typescript + Express

raf*_*adu 7 javascript express typescript reactjs

我正在尝试在我的 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.
Run Code Online (Sandbox Code Playgroud)

我尝试添加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();
Run Code Online (Sandbox Code Playgroud)

小智 9

您必须实例化应用程序和 Express,并使用 const 关键字。

const express = require('express');
const app = express();
Run Code Online (Sandbox Code Playgroud)

另外,在代码库中查找包含以下内容的位置:

import { response } from 'express';
Run Code Online (Sandbox Code Playgroud)

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