expressjs / express

Fast, unopinionated, minimalist web framework for node.
https://expressjs.com
MIT License
65.66k stars 16.26k forks source link

Error message TS2339: Property 'use' does not exist on type 'Router'. #6030

Closed xuetong6 closed 1 day ago

xuetong6 commented 1 month ago

"@types/express": "^5.0.0", "express": "^4.21.0",

import { Router } from "express";
import cors from "cors";

export const handleCors = (router: Router) =>
router.use(cors({ credentials: true, origin: true }));

TS2339: Property 'use' does not exist on type 'Router' Please help me see what the reason is

593-ACD commented 1 month ago

Hey, are you trying to setup cors for the entire app or just a specific route ?

For the entire app it look like this:

import express from "express";
import cors from "cors";

const app = express();

app.use(cors({credentials:true,origin:true}));
xuetong6 commented 1 month ago

const router = express(); applyMiddleware(middleware, router);

My router was passed from another file

NewEraCracker commented 1 month ago

This issue will help you: https://github.com/expressjs/express/issues/5987

You should pin the @types/express to ^4.17.21

dev-gabriel-henrique commented 1 day ago

Try to use: const router = Router() export handleCors = router.use(cors({ credentials: true, origin: true}));

And verify if the @types of express is installed.