question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Help: TypeError: Cannot read property 'passport' of undefined on production build

See original GitHub issue

Bug report

Describe the bug

Everything runs fine on development. The issue happens on production.

‘npm run build’ executes without issues, but once I ‘npm run start’ and visit the page (localhost:3000), I get an “Internal Server Error” on the DOM and my terminal says “TypeError: Cannot read property ‘passport’ of undefined”.

The terminal is pretty much pointing at this being the issue inside _app.js:

    if (ctx.req && ctx.req.session.passport.user) {
      pageProps.user = ctx.req.session.passport.user;
    }

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

I have config and .env file’s required to connect to auth0 and my personal DB, so this may not work. Maybe someone can still help?

  1. Go to My project’s repo: https://github.com/apardo04/stock-market-react-app
  2. clone it
  3. npm install and go to directory
  4. npm run build
  5. npm run start
  6. Go to localhost:3000
  7. See error

Expected behavior

Site runs with no issues

Screenshots

Error I get on terminal: https://imgur.com/a/LwyGUff

System information

  • OS: Both Windows 10 AND ubuntu 18.04
  • Browser (if applies): All of them
  • Version of Next.js: “8.1.0”

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
apardo04commented, Jun 18, 2019

OK, so to fix this new issue I had to run:

"start2": "cross-env NODE_ENV=production node server.js"

instead of:

"start": "next start -p 3000",

This allowed me to go to /login and work normally.

1reaction
robertistokcommented, Jun 17, 2019

@apardo04 After cloning your project and trying to reproduce the bug, I think that the problem might come from the session object not being available on the req.

If you add another check to the if statement you’ve linked, like the line below, it works: ctx.req && ctx.req.session && ctx.req.session.passport

Does anyone have another thought?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Help: TypeError: Cannot read property 'passport' of undefined ...
Describe the bug. Everything runs fine on development. The issue happens on production. 'npm run build' executes without issues, but once I 'npm ......
Read more >
TypeError: Cannot read property 'passport' of undefined nodejs
Your issue is with passport session. You need to use express session (remember to install in your dependencies) before it for it to...
Read more >
Cannot read property 'name' of undefined" laravel passport
Error in render: "TypeError: Cannot read property 'name' of undefined" laravel passport ... Next, we will create a fresh Vue application instance and...
Read more >
Authenticate Users With Node ExpressJS and Passport.js
Create a login form for a Node application using Passport; Use the session ... Then go to the database "users" (and create it,...
Read more >
cannot read properties of undefined (reading 'login') nestjs
Here's an example of a JavaScript TypeError: Cannot read property of undefined thrown when a property is attempted to be read on an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found