fridays / next-routes

Universal dynamic routes for Next.js
MIT License
2.47k stars 230 forks source link

TypeError: Cannot read property 'match' of undefined #47

Closed jesstelford closed 7 years ago

jesstelford commented 7 years ago

I ran across this error today, and was thoroughly confused. I also found the solution, so wanted to share in case others hit the same thing!

The error:

TypeError: Cannot read property 'match' of undefined
    at /node_modules/next-routes/dist/index.js:102:27
    at Layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/node_modules/express/lib/router/index.js:317:13)
    at /node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/node_modules/express/lib/router/index.js:335:12)
    at next (/node_modules/express/lib/router/index.js:275:10)
    at expressInit (/node_modules/express/lib/middleware/init.js:40:5)
    at Layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/node_modules/express/lib/router/index.js:317:13)
    at /node_modules/express/lib/router/index.js:284:7

The cause

I had changed the example code:

server.js

- const routes = require('./routes');
- const handler = routes.getRequestHandler(app);
+ const { getRequestHandler } = require('./routes');
+ const handler = getRequestHandler(app);

Ie; I had introduced a destructruing require, which resulted in the above error.

The fix

Stick with a non-destructured require like the docs show:

server.js

const routes = require('./routes');
// ...
const handler = routes.getRequestHandler(app);