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

TypeError: module.require is not a function error when running with turbopack #8004

Closed
sam-btrt opened this issue Dec 17, 2024 · 6 comments · Fixed by #8010 or #8006
Closed

TypeError: module.require is not a function error when running with turbopack #8004

sam-btrt opened this issue Dec 17, 2024 · 6 comments · Fixed by #8010 or #8006

Comments

@sam-btrt
Copy link

Issue Description

Apologies if this should be an FR, or an issue on a different project. I'm finding it hard to work out if nextjs + turbopack + apollo server is supposed to be supported or not, and what's stopping it from working at the moment.

In any case, when trying to run next dev --turbo on a project with apollo server, I get a bunch of TypeError: module.require is not a function errors whenever the gql endpoint is hit, and no data is returned. Running without --turbo (i.e. using webpack) works fine.

Related issues:
apollo-server-integrations/apollo-server-integration-next#172
apollo-server-integrations/apollo-server-integration-next#232
vercel/next.js#72573

Thanks

Link to Reproduction

See vercel/next.js#72573

Reproduction Steps

See vercel/next.js#72573

@glasser
Copy link
Member

glasser commented Dec 19, 2024

I wanted to look into this, but when I checked out @MersadHabibi's linked repo and ran npm i I got this:

npm error code ERESOLVE
npm error ERESOLVE could not resolve
npm error
npm error While resolving: [email protected]
npm error Found: [email protected]
npm error node_modules/react
npm error   react@"19.0.0-rc-66855b96-20241106" from the root project
npm error   peerOptional react@"^16.8.0 || ^17.0.0 || ^18.0.0 || >=19.0.0-rc <19.0.0" from @apollo/[email protected]
npm error   node_modules/@apollo/client
npm error     @apollo/client@"^3.11.9" from the root project
npm error   5 more (lucide-react, next, react-dom, rehackt, styled-jsx)
npm error
npm error Could not resolve dependency:
npm error peer react@"^16.8.0 || ^17 || ^18 || ^19" from [email protected]
npm error node_modules/react-hook-form
npm error   react-hook-form@"^7.53.2" from the root project
npm error   peer react-hook-form@"^7.0.0" from @hookform/[email protected]
npm error   node_modules/@hookform/resolvers
npm error     @hookform/resolvers@"^3.9.1" from the root project
npm error
npm error Conflicting peer dependency: [email protected]
npm error node_modules/react
npm error   peer react@"^16.8.0 || ^17 || ^18 || ^19" from [email protected]
npm error   node_modules/react-hook-form
npm error     react-hook-form@"^7.53.2" from the root project
npm error     peer react-hook-form@"^7.0.0" from @hookform/[email protected]
npm error     node_modules/@hookform/resolvers
npm error       @hookform/resolvers@"^3.9.1" from the root project
npm error
npm error Fix the upstream dependency conflict, or retry
npm error this command with --force or --legacy-peer-deps
npm error to accept an incorrect (and potentially broken) dependency resolution.

I'd be happy to look in more detail given a full detailed reproduction that requires no creativity on my part. Though it does seem likely that this is just some overzealous optimization on "turbopack"'s part?

@glasser glasser closed this as not planned Won't fix, can't repro, duplicate, stale Dec 19, 2024
@glasser
Copy link
Member

glasser commented Dec 19, 2024

That said, I see createHash in the error messages, which makes me suspect this is related to @apollo/utils.createhash. Looking here, it seems that that package does call module.require after trying to guess if we're in Node. I guess something about this "turbo mode" makes an environment that's kinda but not really like normal Node. Maybe this is fixable.

@glasser glasser reopened this Dec 19, 2024
glasser added a commit to apollographql/apollo-utils that referenced this issue Dec 19, 2024
Apparently Next.JS "Turbopack" runs code in a weird semi-Node
environment that doesn't have module.require. We don't want to call
require directly because that makes other bundlers put in polyfills. So
make Turbopack use sha.js instead of just throwing. Non-ideal
but better than what we have today.

For apollographql/apollo-server#8004
@MersadHabibi
Copy link

I wanted to look into this, but when I checked out @MersadHabibi's linked repo and ran npm i I got this:

npm error code ERESOLVE
npm error ERESOLVE could not resolve
npm error
npm error While resolving: [email protected]
npm error Found: [email protected]
npm error node_modules/react
npm error   react@"19.0.0-rc-66855b96-20241106" from the root project
npm error   peerOptional react@"^16.8.0 || ^17.0.0 || ^18.0.0 || >=19.0.0-rc <19.0.0" from @apollo/[email protected]
npm error   node_modules/@apollo/client
npm error     @apollo/client@"^3.11.9" from the root project
npm error   5 more (lucide-react, next, react-dom, rehackt, styled-jsx)
npm error
npm error Could not resolve dependency:
npm error peer react@"^16.8.0 || ^17 || ^18 || ^19" from [email protected]
npm error node_modules/react-hook-form
npm error   react-hook-form@"^7.53.2" from the root project
npm error   peer react-hook-form@"^7.0.0" from @hookform/[email protected]
npm error   node_modules/@hookform/resolvers
npm error     @hookform/resolvers@"^3.9.1" from the root project
npm error
npm error Conflicting peer dependency: [email protected]
npm error node_modules/react
npm error   peer react@"^16.8.0 || ^17 || ^18 || ^19" from [email protected]
npm error   node_modules/react-hook-form
npm error     react-hook-form@"^7.53.2" from the root project
npm error     peer react-hook-form@"^7.0.0" from @hookform/[email protected]
npm error     node_modules/@hookform/resolvers
npm error       @hookform/resolvers@"^3.9.1" from the root project
npm error
npm error Fix the upstream dependency conflict, or retry
npm error this command with --force or --legacy-peer-deps
npm error to accept an incorrect (and potentially broken) dependency resolution.

I'd be happy to look in more detail given a full detailed reproduction that requires no creativity on my part. Though it does seem likely that this is just some overzealous optimization on "turbopack"'s part?

use npm i --legacy-peer-deps or npm i --force

glasser added a commit to apollographql/apollo-utils that referenced this issue Jan 2, 2025
Apparently Next.JS "Turbopack" runs code in a weird semi-Node
environment that doesn't have module.require. We don't want to call
require directly because that makes other bundlers put in polyfills. So
make Turbopack use sha.js instead of just throwing. Non-ideal
but better than what we have today.

For apollographql/apollo-server#8004

Co-authored-by: Trevor Scheer <[email protected]>
trevor-scheer added a commit to apollographql/apollo-utils that referenced this issue Jan 2, 2025
Apparently Next.JS "Turbopack" runs code in a weird semi-Node
environment that doesn't have module.require. We don't want to call
require directly because that makes other bundlers put in polyfills. So
make Turbopack use sha.js instead of just throwing. Non-ideal
but better than what we have today.

For apollographql/apollo-server#8004

Co-authored-by: Trevor Scheer <[email protected]>
trevor-scheer added a commit to apollographql/apollo-utils that referenced this issue Jan 2, 2025
#468)

Apparently Next.JS "Turbopack" runs code in a weird semi-Node
environment that doesn't have module.require. We don't want to call
require directly because that makes other bundlers put in polyfills. So
make Turbopack use sha.js instead of just throwing. Non-ideal
but better than what we have today.

For apollographql/apollo-server#8004

Co-authored-by: David Glasser <[email protected]>
glasser added a commit that referenced this issue Jan 2, 2025
For compatibility with Next.js Turbopack.

Fixes #8004.
glasser added a commit that referenced this issue Jan 2, 2025
For compatibility with Next.js Turbopack.

Fixes #8004.
glasser added a commit that referenced this issue Jan 2, 2025
For compatibility with Next.js Turbopack.

Fixes #8004.
@glasser
Copy link
Member

glasser commented Jan 2, 2025

Hi, I hope that #8010 fixes this. Because there was no fully detailed reproduction requiring no creativity on my part, I haven't been able to test this. You should be able to test this out from the CodeSandbox prerelease with npm i https://pkg.csb.dev/apollographql/apollo-server/commit/3b13bf4c/@apollo/server. If this fixes the issue, let me know and I'll put out the new release!

@sam-btrt
Copy link
Author

sam-btrt commented Jan 2, 2025

I've confirmed that this fixes the issue, thanks so much!

glasser added a commit that referenced this issue Jan 3, 2025
For compatibility with Next.js Turbopack.

Fixes #8004.
glasser added a commit that referenced this issue Jan 3, 2025
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @apollo/[email protected]

### Patch Changes

- [#8010](#8010)
[`f4228e8`](f4228e8)
Thanks [@glasser](https://github.com/glasser)! - Compatibility with
Next.js Turbopack. Fixes #8004.

- Updated dependencies
\[[`f4228e8`](f4228e8),
[`70eecce`](70eecce)]:
    -   @apollo/[email protected]

## @apollo/[email protected]

### Patch Changes

- [#8010](#8010)
[`f4228e8`](f4228e8)
Thanks [@glasser](https://github.com/glasser)! - Compatibility with
Next.js Turbopack. Fixes #8004.

## @apollo/[email protected]

### Patch Changes

- [#8010](#8010)
[`f4228e8`](f4228e8)
Thanks [@glasser](https://github.com/glasser)! - Compatibility with
Next.js Turbopack. Fixes #8004.

- [#8005](#8005)
[`70eecce`](70eecce)
Thanks [@Meschreiber](https://github.com/Meschreiber)! - Remove Discord
link from README.md and templates

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: David Glasser <[email protected]>
@glasser
Copy link
Member

glasser commented Jan 3, 2025

OK, then this should be fixed in @apollo/[email protected].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants