error - ReferenceError: Cannot access 'createTRPCRouter' before initialization
Anyone ever experience an error as in title of this Q.
src\server\api\root.ts
If you change import order and move roleRouter import at top, it will throw error at roleRouter in below error description.
Error Description:
16 Replies
The error message "Cannot access 'createTRPCRouter' before initialization" suggests that the function is being called before it has been properly initialized. This could be caused by a variety of issues in the code, such as a missing import statement, a circular dependency, or a lack of proper initialization logic.
What have you done to look into this before posting?
Currently having same problem. It disappears after doing any change in any file and saving it. But when I rerun the project it comes back.
Here is how browser shows my error
I have look into https://github.com/trpc/trpc/issues/3081 trpc repo. issue but coudn't figure out exact issue. It returns me 500 error with text/html response.
I'm signing in using nextauth discord/google provider, and redirecting user to callbackUrl (dashboard), then calling some trpc routes to get permissioned navigation menu items. but trpc throws 500 with html text response.
GitHub
httpLink/httpBatchLink on fetch redirect · Issue #3081 · trpc/trpc
Provide environment information System: OS: macOS 11.6.8 CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz Memory: 610.94 MB / 32.00 GB Shell: 5.8 - /bin/zsh Binaries: Node: 18.11.0 - ~/.nvm/v...
Not sure how that issue relates. You may need to provide a repro with that error message (the imports dont count as a repro) & the steps to reproduce.
https://stackoverflow.com/help/minimal-reproducible-example
Stack Overflow
How to create a Minimal, Reproducible Example - Help Center
Stack Overflow | The World’s Largest Online Community for Developers
i found the line that causes the error but idk how else should i tackle this issue. So i wanted only approved accounts to log in so i made a signin callback which calls trpc to check if requesting user is approved (sort of a whitelist).
If you change import order and move roleRouter import at top, it will throw error at roleRouter in below error description.
import { createTRPCRouter } from "./trpc";
Likely needs to be first to initialize the Router, import order can matter.I am getting a feeling this is a different issue
without this line, it works
I needed to put import of createInnerTRPCContext above import of usersRouter in [...nextauth].ts
now it works form me
thanks for guiding me on the badly ordered imports trope
lol
I have found exact issue which is causing an error. you can't use "createTRPCContext" inside next auth signIn callback even if you wrap auth options to access NextApiRequest and NextApiResponse, I need to wrap it because i need some cookie values.
Using "createInnerTRPCContext" does fix an error:
Code (signIn callback lines produce unusual error as mentioned in title):
@0𝙚𝙪𝙚 ✓ @JacobMGEvans Do you think using "createTRPCContext" anywhere should not cause any issue?
I have created latest create t3 app: https://github.com/nimeshvaghasiya/t3routererror
Steps to reproduce error:
- Clone above repository, do npm install then npm run dev
- Click on "Roles"
- Click on "Sign in" button : throws error
- If you comment out signin callback, there will be no error.
@JacobMGEvans @0𝙚𝙪𝙚 ✓ I would like to open an issue to t3app github repo, if someone can confirm this is a real issue.
GitHub
GitHub - nimeshvaghasiya/t3routererror
Contribute to nimeshvaghasiya/t3routererror development by creating an account on GitHub.
Just open an issue, if its not an issue to them they will tell you.
Any updates? I'm having the same issue
@Stivo please refer this: https://github.com/t3-oss/create-t3-app/issues/1093
GitHub
bug: error - ReferenceError: Cannot access 'createTRPCRouter' befor...
Provide environment information System: OS: Windows 10 10.0.19045 CPU: (4) x64 Intel(R) Core(TM) i5-7300HQ CPU @ 2.50GHz Memory: 3.85 GB / 11.87 GB Binaries: Node: 16.16.0 - D:\Program Files\nodejs...