next.js: ReferenceError: e is not defined error occurs when running next dev --turbo
Link to the code that reproduces this issue
https://github.com/y-hsgw/with-urql
To Reproduce
When using next dev --turbo with a Next.js application, the following error message appears:
ReferenceError: e is not defined
at Module.Kind (/path/to/.next/server/chunks/ssr/node_modules_xyz.js:1376:18)
at Kind (turbopack://[project]/node_modules/@urql/core/src/gql.ts:84:30)
...
The application works without issues when running the next dev command.
Current vs. Expected behavior
bug screenshot:
Provide environment information
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 24.0.0: Tue Sep 24 23:36:26 PDT 2024; root:xnu-11215.1.12~1/RELEASE_ARM64_T8103
Available memory (MB): 8192
Available CPU cores: 8
Binaries:
Node: 22.10.0
npm: 10.9.0
Yarn: 4.5.1
pnpm: N/A
Relevant Packages:
next: 15.0.2 // Latest available version is detected (15.0.2).
eslint-config-next: N/A
react: 18.3.1
react-dom: 18.3.1
typescript: 5.6.3
Next.js Config:
output: N/A
Which area(s) are affected? (Select all that apply)
Turbopack
Which stage(s) are affected? (Select all that apply)
next dev (local)
Additional context
I also submitted an issue to urql, but they responded that it was an issue on the Next.js side.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 20
- Comments: 30 (11 by maintainers)
Commits related to this issue
- fix: :sparkles: accept multiple status and origins doesn't work with turbopack see: https://github.com/vercel/next.js/issues/72232 — committed to polkassembly/polkassembly-v2 by alphainfinitus 2 years ago
- feat: :rotating_light: added dev command without turbopack using classic dev (without turbopack) because of https://github.com/vercel/next.js/issues/72232 — committed to polkassembly/polkassembly-v2 by alphainfinitus 2 years ago
- fix(turbopack): Use correct `SyntaxContext` for `__turbopack_esm__` (#73544) ### What? ### Why? For description about the `resolver`/`hygiene` system, please see https://rustdoc.swc.rs/swc_core/ecm... — committed to ZainCheung/next.js by kdy1 2 years ago
I’ll work on this issue (and swc/turbopack bufixes) starting from the tomorrow.
Would love to help or get this fixed!
Alright, looked into this some more. I’ll summarize my findings and then we can maybe discuss where this should be reported, since I don’t believe it’s something that Turbopack can fix/should attempt to fix.
I’ll be referring to the following code snippets (all links are permanent links to the current head-commits on their respective master/main branch, as of writing this):
DocumentNode from graphql-js. This has gained a new property
tokenCountyesterday which you can ignore for the purposes of this discussion. What’s important is that it has 3readonlyfields, of which one, calledloc, is optional.DocumentNode from graphql.web is funamentally identical to the graphql-js, version. It’s just a type definition that says
DocumentNodecan either be of the graphql-js variety or of the graphql.web variety.DocumentNode from urql. Their type definition says DocumentNode is of the DocumentNode type from graphql.web, but not of the graphql-js type.
parse from qraphql.web. This can take a string and parse it into a DocumentNode. Notably it can take
ParseOptionswhich can tell the next method mentioned below that thelocproperty should remain undefined on theDocumentNode.document from graphql.web. This takes an input string and a boolean that specifies whether no location information should be added and constructs a DocumentNode.
stringifyDocument from urql. The premise of this method shouldn’t introduce any problems: take a DocumentNode and turn it into a string. The problem that it introduces can be found in the method descriptions, which reads “When a DocumentNode is passed to this function, it caches its output by modifying the
loc.source.bodyproperty on the GraphQL node.”.This IMHO is a flawed function, because it assumes there is a place in the
DocumentNodewhere information can always be cached. It does a little checking with!node.locbut then throws all caution out the window with an assignment to a(node as any).loccast. This initially worked fine: the!node.loccheck correctly identified whetherDocumentNodeeven has alocand would block assignment if no such property existed.It worked until it did not work anymore: This pull request added a getter to
DocumentNodefor the Ono-co/gql.tada GraphQL query engine, which got backported to graphql.web last week for compatibility reasons.Having a getter for a potentially undefined property causes problems, especially if you use “property is accessible” as an indicator for “property is assignable”. And so it crashed, resulting in this gql.tada issue, where the stacktrace indicates that it crashed exactly in
stringifyDocument.All of this of course completely ignores that
locwas originally meant to bereadonly. If it exists fine, you can alter the properties ofLocationassigned toloc, since those aren’treadonly, but otherwise you probably shouldn’t mess with caching information in an undefined variable.Anyways, instead of overthinking this entire situation adding a setter was the chosen solution that worked for them, but that now just propagates the issue to turbopack users. Turbopack (rightfully) assumes
readonlyproperties shouldn’t have a setter and thus, during compilation, apparently discards that function. When it comes to putting the compilation result back together in the browser of the user, the “ReferenceError: e is not defined” error from the title of this issue gets thrown.How I would fix this problem: roll back the introduction of the setter and getter and improve the stringify method in two ways:
locis actually not undefined.Locationinstead of assigning a newLocationto the originallyreadonlyproperty. This would get rid of the(node as any)cast too.Something I have not fully understood yet is the entire
graphql-tagsituation described in the pull request that introduced the getter, but maybe they could fix the problem on theirgraphql-tagend in a different way that doesn’t involve changes to gql.tada (and by extension graphql.web).Overall there are A LOT of parties/people involved in this entire situation and turbopack users are simply on the receiving end of an unfortunate situation. Following the chain of dependencies I believe a good place to start would be reporting the problem to urql, who could then decide whether this is something they would want to improve. This time it’s turbopack, but next time it could be some other dependency/project that runs into trouble with urql.
Reporting an issue to urql involves creating a reproduction, which I wouldn’t mind doing but I first wanted to hear the opinions of other people here.
I’m getting the same error with
gql.tada.I think the problem is related to
@0no-co/graphql.webv1.0.10+ whichurqlis also using internally.If I pin
@0no-co/graphql.webto v1.0.9, it works fine.One of the following PRs is introducing the problem:
To add some color here, this is impacting any package that uses either of those
urqlorgql.tada(500K+ weekly installs)Furthermore
turbois now the default setting withNext.js15 and above.It seems that if users previously picked
noon theturbosetting, the cli remembers that – so they will not experiencing issues on subsequent installs by default.But e.g. any first-time Next.js user (or a user on a new machine) will see broken behavior on the default path for lots of packages.
@nphmuller Thank you so much for the investigation! It looks like turbopack is not following the strict rule of
resolver/hygienesystem of SWC and I’ll fix it soon.Through bisection i figured out that it’s a problem with
Specifically these five lines that introduce a setter cause the problem.
Removing those lines even in the current master branch HEAD of the /0no-co/graphql.web project and building the project results in a
distresult that doesn’t throw an error when used with tubopack.I assume the broader problem is that the document function returns ast.DocumentNode, which in turn uses a custom Or type to express that it’s either their own DocumentNode or a DocumentNode from graphql-js.
Both of those DocumentNode have an optional property
readonly loc?: Location;, which the defined getter and setter don’t refer to though, instead they work with this scoped variable loc from the document function.The combination of getter and setter existing for an (optional) property that is otherwise never used, in combination witth the getter and setter not referring to the actual property of ast.DocumentNode is probably something Turbopack can’t handle.
Issue still persists after updating to Next
15.2.2andgql.tadaWell, I’m on vacation until 1/1
@kdy1 It’s been a bit over a month since your initial response to this thread. We have a bunch of users who integrate with Next.js and turbopack adoption seems to be increasing. We use urql as a dependency, which means most new users get this
e is not definederror. Do you have any updates please? Or any quick hacks/mitigation steps we can do on our end?Only the esm export of @0no-co/graphql.web has the issue above. Is there a way to force turbopack to choose the cjs output for a package?
The only way I get it working is via the patch below (using https://www.npmjs.com/package/patch-package):
Issue still happening in
nextversion15.3.0, usinggql.tada, but the patch above is a solutionJust came across this, any progress on the solution @suhasdeshpande @kdy1 ?
Was testing this again with
v15.2.2-canary.3, which included @kdy1’s PR…But still seems to be giving off an error on the same vibe 😕. Works perfectly without the
--turbo.Edit: Also, using this patch suggestion https://github.com/vercel/next.js/issues/72232#issuecomment-2514184986 seems to be working here 🙇
Any guidelines for how contributors can help fix this? My team is really wanting to make some progress here, we’re happy to help in whatever way that we can.