Skip to content

TypeError: Cannot read property 'map' of undefined at Server.generateRoutes  #9970

Closed

Description

@raguiar2

Bug report

Describe the bug

I've been using nextjs with typescript to build a web app (based off of the example at https://github.com/mui-org/material-ui/tree/master/examples/nextjs-with-typescript). However, when I deploy my app to an AWS elastic beanstalk instance, I get a strange error that breaks my app. next start does not run correctly. I cannot reproduce this locally, only when I deploy to AWS's elastic beanstalk.

Stack trace:

> next start --port 8081

TypeError: Cannot read property 'map' of undefined
    at Server.generateRoutes (/var/app/current/node_modules/next/dist/next-server/server/next-server.js:257:36)
    at new Server (/var/app/current/node_modules/next/dist/next-server/server/next-server.js:81:49)
    at createServer (/var/app/current/node_modules/next/dist/server/next.js:2:133)
    at start (/var/app/current/node_modules/next/dist/server/lib/start-server.js:1:323)
    at nextStart (/var/app/current/node_modules/next/dist/cli/next-start.js:22:125)
    at commands.(anonymous function).then.exec (/var/app/current/node_modules/next/dist/bin/next:29:346)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! test-website@1.0.0 start: `next start --port 8081`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the test-website@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

To Reproduce

Deploy nextjs app with typescript to elastic beanstalk instance

Expected behavior

The app should just run next start normally.

System information

  • OS: Node.js running on 64bit Amazon Linux/4.12.0
  • Version of Next.js: both 9.1.16 and 9.1.17

Activity

  1. takashi-yokomichi commented on Jan 7, 2020

    @takashi-yokomichi

    Same error happen.
    This error may only happen if we start up server with custom server.
    https://nextjs.org/docs/advanced-features/custom-server

    Temporary I've resolve the issue by decrease next.js version to 9.1.14

  2. mogaming217 commented on Jan 7, 2020

    @mogaming217

    This error occued in next.js 9.1.6 too.

  3. timneutkens commented on Jan 7, 2020

    @timneutkens
    Contributor

    A full reproduction is needed.

  4. added
    please add a complete reproduction
    on Jan 7, 2020
  5. adamthewan commented on Jan 10, 2020

    @adamthewan

    Facing this issue as well and downgraded to 9.1.4

  6. timneutkens commented on Jan 10, 2020

    @timneutkens
    Contributor

    @adamthewan please provide a full reproduction otherwise your comment really is not helpful in resolving the issue.

  7. adamthewan commented on Jan 10, 2020

    @adamthewan

    Yup sorry about that.

    My build environment is inside Google Cloud Functions

    System information

    OS: Node.js 10 (Beta)
    Version of Next.js: 9.1.7

    My build is something like this:

    const next = require('next');
    const path = require('path');
    
    exports.app = functions.https.onRequest(async (req, res) => {
      const nextApp = next({
        dev: false,
        conf: {
          distDir: `${path.relative(process.cwd(), __dirname)}/dist/prod/.next`,
        },
      });
    
        await nextApp.prepare();
        const handle = nextApp.getRequestHandler();
        handle(req, res);
      
    });
    

    Describe the bug

    When accessing the endpoint, the server will crash with this log:

    TypeError: Cannot read property 'map' of undefined
        at Server.generateRoutes (/srv/functions/node_modules/next/dist/next-server/server/next-server.js:257:36)
        at new Server (/srv/functions/node_modules/next/dist/next-server/server/next-server.js:81:49)
        at createServer (/srv/functions/node_modules/next/dist/server/next.js:2:133)
        at exports.app.functions.https.onRequest (/srv/functions/index.js:12:19)
        at cloudFunction (/srv/functions/node_modules/firebase-functions/lib/providers/https.js:49:9)
        at process.nextTick (/srv/node_modules/@google-cloud/functions-framework/build/src/invoker.js:243:17)
        at process._tickCallback (internal/process/next_tick.js:61:11)
    
  8. timneutkens commented on Jan 10, 2020

    @timneutkens
    Contributor

    I'm going to post the exact same reply. This time with bold text.

    Please provide a full reproduction otherwise the comment really is not helpful in resolving the issue.

  9. vaibhav269 commented on Jan 14, 2020

    @vaibhav269

    facing the same issue

  10. timneutkens commented on Jan 14, 2020

    @timneutkens
    Contributor

    @vaibhav269 this is really not helpful in resolving the issue. As said:

    Please provide a full reproduction otherwise the comment really is not helpful in resolving the issue.

  11. vaibhav269 commented on Jan 14, 2020

    @vaibhav269

    This is the package.json file

    {
      "name": "darecase",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "dependencies": {
        "@fortawesome/fontawesome-svg-core": "^1.2.26",
        "@fortawesome/free-solid-svg-icons": "^5.12.0",
        "@fortawesome/react-fontawesome": "^0.1.8",
        "cookie-parser": "^1.4.4",
        "express": "^4.17.1",
        "isomorphic-unfetch": "^3.0.0",
        "js-cookie": "^2.2.1",
        "mysql": "^2.17.1",
        "next": "9.1.4",
        "rand-token": "^0.4.0",
        "react": "^16.12.0",
        "react-dom": "^16.12.0"
      },
      "devDependencies": {
        "styled-jsx": "^3.2.4"
      },
      "scripts": {
        "dev": "nodemon index.js",
        "build": "next build",
        "start": "NODE_ENV=production node index.js"
      },
      "keywords": [],
      "author": "",
      "license": "ISC"
    }
    

    The server file is

    const next = require('next');
    const express = require('express');
    const uid = require('rand-token').uid;
    const dev = process.env.NODE_ENV !== 'production';
    const port = process.env.port || 3000;
    const con = require('./config/db');
    const cookieParser = require('cookie-parser');
    
    const app = next({dev});
    const handle = app.getRequestHandler();
    
    app.prepare().then(()=>{
        const server = express();
        server.use(express.json());
        
        server.use(cookieParser());
    
        server.get('*',(req,res)=>{
            return handle(req,res);
        });
    
        server.listen(port,err=>{
            if(err){
                throw err;
            }else{
                console.log("server running at port",port);
            }
        })
    })
    

    Everything works fine in local server but when i try to host the application to the server in production mode i get this error on running 'npm run start'
    Note: The application works fine in the development mode too... I am facing the error only in the case of production mode.

    TypeError: Cannot read property 'map' of undefined
        at Server.generateRoutes (/var/app/current/node_modules/next/dist/next-server/server/next-server.js:257:36)
        at new Server (/var/app/current/node_modules/next/dist/next-server/server/next-server.js:81:49)
        at createServer (/var/app/current/node_modules/next/dist/server/next.js:2:133)
        at start (/var/app/current/node_modules/next/dist/server/lib/start-server.js:1:323)
        at nextStart (/var/app/current/node_modules/next/dist/cli/next-start.js:22:125)
        at commands.(anonymous function).then.exec (/var/app/current/node_modules/next/dist/bin/next:29:346)
        at process._tickCallback (internal/process/next_tick.js:68:7)
        at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
        at startup (internal/bootstrap/node.js:283:19)
        at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
    npm ERR! code ELIFECYCLE
    npm ERR! errno 1
    npm ERR! test-website@1.0.0 start: `next start --port 8081`
    npm ERR! Exit status 1
    npm ERR! 
    npm ERR! Failed at the test-website@1.0.0 start script.
    npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
    

    Fixed the issue by sticking to the version v9.4.1 i.e by removing the '^' in "next": "^9.1.4" in package.json

    Hope that helps :)

  12. timneutkens commented on Jan 14, 2020

    @timneutkens
    Contributor

    Please provide a full reproduction including all code. Just 2 files is not enough.

  13. vaibhav269 commented on Jan 14, 2020

    @vaibhav269

    Sorry but i can't reveal the full code. Try it out according to me given files are enough to reproduce the error.

    1. Run 'npm install'
    2. Run 'npm run build'
    3. Host the application to a server
    4. Run 'npm run start'

    You will probably get the error

  14. timneutkens commented on Jan 15, 2020

    @timneutkens
    Contributor

    @vaibhav269 Just tried it with the provided files and steps and I could not reproduce.

  15. modamodamoda commented on Jan 24, 2020

    @modamodamoda

    I was able to fix the issue simply with
    rm -rf .next (i.e. clearing the "cached" files) and then building anew with npm run build

    I will say one thing however, there's a lot of issues I've been having with next.js and whenever I find the bug posted here the development team always seem to deny any issues.

    In this case, it's not really a bug with next.js, but perhaps advising to clear the cache and rebuild would be more constructive than "Cannot reproduce. Next.js can do no wrong."

  16. timneutkens commented on Jan 24, 2020

    @timneutkens
    Contributor

    I will say one thing however, there's a lot of issues I've been having with next.js and whenever I find the bug posted here the development team always seem to deny any issues.

    We don't deny issues. I've already spent over an hour on this particular issue without being able to reproduce as no reproduction was provided.

    We ask for a reproduction so that we can add a test in our test suite to ensure it doesn't happen again.

    Eg in this case I've asked for a full reproduction 5 times and 1 time it was provided, tried to reproduce based on the steps provided and couldn't reproduce so posted back. Didn't hear back from anyone experiencing the issue since.

    In this case, it's not really a bug with next.js, but perhaps advising to clear the cache and rebuild would be more constructive than "Cannot reproduce. Next.js can do no wrong."

    Specifically "Next.js can do no wrong"

    I've never said that, I've only asked for a reproduction so that we can actually solve the issue you're experiencing.

  17. barayuda commented on Jul 21, 2020

    @barayuda

    Sorry, jump to this.
    I faced this same error.
    in pages/index.js, I used .map to looping fetch API response and build to Netlify.
    When I deploy it, I got this error.
    Screenshot below:
    http://prnt.sc/tlunq7

  18. manuarora700 commented on Nov 2, 2020

    @manuarora700

    Sorry, jump to this.
    I faced this same error.
    in pages/index.js, I used .map to looping fetch API response and build to Netlify.
    When I deploy it, I got this error.
    Screenshot below:
    http://prnt.sc/tlunq7

    I'm facing the same issue on Vercel. I'm trying to map over an array in getStaticPaths and it throws the same error.

  19. balazsorban44 commented on Jan 29, 2022

    @balazsorban44
    Contributor

    This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

  20. locked as resolved and limited conversation to collaborators on Jan 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    please add a complete reproduction

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions