最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: Cannot read properties of undefined (reading 'prototype') React Typescript + Express - S

programmeradmin3浏览0评论

I'm trying to use express at my React-Typescript SPA. But It's giving me the error:

    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.

I've tried adding target: "node" to my "node_modules/react-scripts/config/webpack.config.js" But it won't work...

Here's some ways I've already tried to solve it:

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

I'm trying to use express at my React-Typescript SPA. But It's giving me the error:

    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.

I've tried adding target: "node" to my "node_modules/react-scripts/config/webpack.config.js" But it won't work...

Here's some ways I've already tried to solve it:

    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();
Share Improve this question asked Sep 2, 2021 at 1:49 rafaelpadurafaelpadu 1,6623 gold badges10 silver badges27 bronze badges 8
  • I think the problem is you forgot to instantiate app with const, var, or let for the first example, since it should work. – Kenneth Lew Commented Sep 2, 2021 at 1:57
  • duplicate: stackoverflow.com/questions/56880311/… – Kenneth Lew Commented Sep 2, 2021 at 1:59
  • @KennethLew sorry I copied incorrectly, but my I was using const app = express() – rafaelpadu Commented Sep 2, 2021 at 2:22
  • @KennethLew I saw that post, but It didn't work for me... – rafaelpadu Commented Sep 2, 2021 at 2:23
  • perhaps try import * as express from "express" – Kenneth Lew Commented Sep 2, 2021 at 2:27
 |  Show 3 more comments

3 Answers 3

Reset to default 9

You have to instantiate both the app and express, use the const keyword as well.

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

Also, look for anywhere in your code base where you have:

import { response } from 'express';

Often this is put in by mistake and removing this line may solve the error

Faced same issue

it was imported under

import e from "express";

not sure cuz of any random IntelliSense extension or maybe the GH copilot auto-completion

faced the same issue

var res = Object.create(http.ServerResponse.prototype)

change it to

let res = Object.create(http.ServerResponse.prototype)

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论