Help: TypeError: Cannot read property 'passport' of undefined on production build
See original GitHub issueBug 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?
- Go to My project’s repo: https://github.com/apardo04/stock-market-react-app
- clone it
- npm install and go to directory
- npm run build
- npm run start
- Go to localhost:3000
- 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:
- Created 4 years ago
- Comments:5
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

OK, so to fix this new issue I had to run:
instead of:
This allowed me to go to /login and work normally.
@apardo04 After cloning your project and trying to reproduce the bug, I think that the problem might come from the
sessionobject not being available on thereq.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.passportDoes anyone have another thought?