typescriptexpresspath-to-regexp

How to solve path-to-regexp dependency issue in Express 5.1.0?


I created this simple file:

import express from "express";
import type { Request, Response } from "express";
const app = express();
const port = 3000;
app.get('/', (req:Request, res:Response) => {
res.send('Hello World!');
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
app.all('*', (req, res) => {
res.status(404).send('Not Found');
});

and ran it with node index.ts.

I still have the same error:

(node:36496) ExperimentalWarning: Type Stripping is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
C:\Users\PASARGAD\web_practice\ouro_server\node_modules\path-to-regexp\src\index.ts:153
      throw new TypeError(`Missing parameter name at ${i}: ${DEBUG_URL}`);
            ^
TypeError: Missing parameter name at 1: https://git.new/pathToRegexpError
    at name (C:\Users\PASARGAD\web_practice\ouro_server\node_modules\path-to-regexp\src\index.ts:153:13)
    at lexer (C:\Users\PASARGAD\web_practice\ouro_server\node_modules\path-to-regexp\src\index.ts:171:21)
    at lexer.next (<anonymous>)
    at Iter.peek (C:\Users\PASARGAD\web_practice\ouro_server\node_modules\path-to-regexp\src\index.ts:188:32)
    at Iter.tryConsume (C:\Users\PASARGAD\web_practice\ouro_server\node_modules\path-to-regexp\src\index.ts:195:24)
    at Iter.text (C:\Users\PASARGAD\web_practice\ouro_server\node_modules\path-to-regexp\src\index.ts:213:26)
    at consume (C:\Users\PASARGAD\web_practice\ouro_server\node_modules\path-to-regexp\src\index.ts:285:23)
    at parse (C:\Users\PASARGAD\web_practice\ouro_server\node_modules\path-to-regexp\src\index.ts:320:18)
    at C:\Users\PASARGAD\web_practice\ouro_server\node_modules\path-to-regexp\src\index.ts:503:40
    at Array.map (<anonymous>)

This is the package file:

{
"name": "shop_server",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"type": "module",
"scripts": {
"dev": "nodemon src/index.ts",
"build": "tsc"
},
"dependencies": {
"@prisma/client": "^6.6.0",
"dotenv": "^16.5.0",
"express": "^5.1.0",
"joi": "^17.13.3",
"nodemon": "^3.1.9"
},
"devDependencies": {
"@types/express": "^5.0.1",
"@types/node": "^22.14.1",
"prisma": "^6.6.0",
"ts-node": "^10.9.2",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.5.3"
},
"private": true
}

This is tsconfig.json:

{
  "compilerOptions": {
    "target": "es2022",
    "module": "NodeNext",
    "esModuleInterop": true,
    "strict": true,
    "skipLibCheck": true,
    "outDir": "dist",
    "lib": ["es2022", "dom"],
    "allowImportingTsExtensions": true,
    "noEmit": false,
    "verbatimModuleSyntax": true,
    "moduleResolution": "NodeNext",
    "rootDir": "src"
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"]
}

How do I fix the problem?


Solution

  • In Express 4 and earlier, app.all('*') acted as a catch-all route for all requests without the need to name the wildcard. However, in Express 5, it seems that the unnamed wildcard is no longer allowed and must be named.

    app.get('/', (req: Request, res: Response) => {
        res.send('Hello World!');
    });
    
    app.all('/*splat', (req: Request, res: Response) => {
        res.status(404).json({
            message: `The URL ${req.originalUrl} doesn't exist`
        });
    });