Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collecting page data ...TypeError: Cannot read property 'getInitialProps' of undefined #5978

Closed
3 tasks done
a-ursino opened this issue Oct 17, 2022 · 7 comments · Fixed by #5984 or #8615
Closed
3 tasks done

Comments

@a-ursino
Copy link

a-ursino commented Oct 17, 2022

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which package are you using?

@sentry/nextjs

SDK Version

7.15

Framework Version

No response

Link to Sentry event

No response

Steps to Reproduce

clone https://github.com/a-ursino/nextjs-sentry-bug
yarn
yarn build

Verify errors in the build output.

However if I don't import the content of the homepage from src/pages/Homepage and just inline it in pages/index.ts everything works fine.
If I disable sentry from next.config.js, I can still import the content from src/pages/Homepage and the build works fine.

NOTE:
I've created the example website with
npx create-next-app@latest
npx @sentry/wizard -i nextjs

Expected Result

build to succeed

Actual Result

info  - Linting and checking validity of types
info  - Creating an optimized production build
info  - Compiled successfully
info  - Collecting page data ...TypeError: Cannot read property 'getInitialProps' of undefined
    at Object.355 (/Users/xyz/dev/-personal/my-app/.next/server/pages/index.js:180:41)
@lforst
Copy link
Member

lforst commented Oct 18, 2022

Hi, thanks for writing in and providing a reproduction example. It helped a lot in debugging this.

There was indeed a bug in our logic with the src directory Next.js lets users put the pages into.

I opened #5984 to fix this.

@joelbiffin
Copy link

Hi @lforst, unfortunately I'm experiencing this issue too.

Version: 7.22.0 (7.21.1 didn't work either)

Repository setup:

  • Used the wizard to define next.config.js
  • All pages are under src/pages and there is no /pages directory at the root of the project. My understanding is that these src/pages should be treated as the app's pages.

Contents of next.config.js:

const { withSentryConfig } = require('@sentry/nextjs');

const moduleExports = {
  sentry: {
    hideSourceMaps: true,
  },
};

const sentryWebpackPluginOptions = { silent: true };

module.exports = withSentryConfig(moduleExports, sentryWebpackPluginOptions);

Running next build produces the following output:

 $ yarn next build
yarn run v1.22.19
$ /Users/joel/Code/the_room/theroom-frontend/node_modules/.bin/next build
info  - Loaded env from /Users/joel/Code/the_room/theroom-frontend/.env
info  - Checking validity of types  
info  - Disabled SWC as replacement for Babel because of custom Babel configuration ".babelrc" https://nextjs.org/docs/messages/swc-disabled
info  - Using external babel configuration from /Users/joel/Code/the_room/theroom-frontend/.babelrc
info  - Creating an optimized production build  
info  - Compiled successfully

> Build error occurred
TypeError: Cannot read properties of undefined (reading 'getInitialProps')
    at Object.49332 (/Users/joel/Code/the_room/theroom-frontend/.next/server/pages/brand/account/password.js:227:65)

From what I can see, the patch referenced on this issue should wrap either pages underneath /pages or src/pages (provided that no /pages dir exists). Am I correct in thinking this?

@lforst
Copy link
Member

lforst commented Nov 30, 2022

@joelbiffin Thanks for reporting this! Would you be able to provide a reproduction example? It's hard debugging this from the information you shared.

@joelbiffin
Copy link

Thanks @lforst for getting back so quickly! So, after digging through a few more of the Github Issues, it looks like there is already an open issue here.

I tried reproducing the issue from create-next-app but I wasn't able to successfully recreate the problem. I'll try again later this afternoon when I get some more time and update the other issue with the status.

Notably though, downgrading to 7.12.1 resolved the build issue as some of the commenters on vercel/next.js#36221 suggest.

@korac
Copy link

korac commented Jul 14, 2023

Jumping in on this one.

I experienced same issue with Next 12.x.
What I noticed is that the bug was introduced in @sentry/nextjs: 7.17.1 (specifically #6013). In a nutshell, regex update caused the bug. My project contained pages folder as well as one extra, pages-components folder and this regex update matched both of these folders instead of pages only!

From my project's perspective, quick solution was to rename the pages-components to something else that doesn't start with name pages. ✅

Check out my comment in Next.js GH thread for more info.

In addition, I took a quick glimpse at the same file (nextjs/src/config/webpack.ts) in the latest package version (7.58.1) and I believe the bug lurks in this line of code:

normalizedAbsoluteResourcePath.startsWith(pagesDirPath) &&

which matches all folders starting with pages and src/pages.

Since I haven't heard that Next discourages creation of custom folders that start with the name pages, I believe this is a good candidate for a fix.

Hope that was helpful!

@lforst
Copy link
Member

lforst commented Jul 24, 2023

@korac that's actually super helpful. I opened a PR to fix: #8615

@korac
Copy link

korac commented Jul 24, 2023

@korac that's actually super helpful. I opened a PR to fix: #8615

Glad to help! Lmk if there's any need to assist. Would be happy to lend a hand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment