skip to Main Content

I am running into this issue while I try to run my backend NestJs application using ‘npm run start:dev’. I get the error: TSError: x Unable to compile typescript: Property ‘url’ does not exist on type ‘Request’.
The application was running fine a few days ago and I am not sure what I did that caused this issue.
I am also getting: Property ‘status’ does not exist on type ‘Request’

Both of these issues are coming from the main.ts file.
This is the main.ts file:

import { Request, Response } from 'express';
async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  app.enableCors();
  app.setGlobalPrefix('api');
  app.use(json({ limit: '50mb' }));
  app.use(passport.initialize());
  app.use(passport.session());

  app.use('/', (req: Request, res: Response, next) => {
    if (req.url === '/') {
      return res.status(200).json({ message: 'api base route.' });
    }
    next();
  });

}
bootstrap(); 

I tried updating the node version to 18.16.4 and I also updated the nest version to v8. I uninstalled and reinstalled ts-node and typescript, express and @types/express versions.
But still getting the same issue.

2

Answers


  1. Check the express and @types/express versions are same or not. Should match these versions. If not match these versions try downgrading or upgrading @types/express version.

    Login or Signup to reply.
  2. You can import UrlWithParsedQuery and StatusCodes in your code and You need to use them instead of Request’s url and status properties

    I have just updated your existing code. You can try with this below :-

    import { Request, Response } from 'express';
    import { UrlWithParsedQuery } from 'url';
    import { StatusCodes } from 'http-status-codes';
    
    async function bootstrap() {
    
      const app = await NestFactory.create(AppModule);
    
      app.enableCors();
    
      app.setGlobalPrefix('api');
      app.use(json({ limit: '50mb' }));
      app.use(passport.initialize());
      app.use(passport.session());
    
      app.use('/', (req: Request, res: Response, next) => {
    
        const url = req.originalUrl as UrlWithParsedQuery;
        if (url.pathname === '/') {
    
            return res.status(StatusCodes.OK).json({ message: 'api base route.' });
        }
        next();
      });
    }
    
    bootstrap();
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search