T3 Stack + custom Next.js server not working
Hi, I'm using t3 stack and trying to leverage Next.js custom server. But, unfortunately when I'm trying to run it using
ts-node
I'm constantly getting an error connected to module resolution (I guess it's because my next.config.mjs
has .mjs
extension):
What should I do to fix this? Here's the full code:
https://github.com/Bartek532/rssmarkable/pull/40
Thanks in advance for all suggestions! 🙂21 Replies
Any thoughts on this one?
Struggling with this one too, I can either get
next build
to run or my custom server, but never both. Would love to see an example project where prod actually works with a custom server.I got this working. I’m not at my computer right now, but happy to share the steps I took when I’m back
Would love to see! I also now have a working example but I'm unsure If my setup is ideal
@james and @moloch would love to see both of your working examples. I bet several others in the community would as well!
Will polish it up tomorrow and share the repo here (as long as I remember)
These are the steps I took:
1. Install dependencies:
2. Update package.json scripts:
3. Add nodemon.json file:
4. Update tsconfig.json
"checkJs": true
to "checkJs": false
5. Add tsconfig.server.json file:
6. Add the server.ts file:
Sources:
- https://nextjs.org/docs/advanced-features/custom-server
- https://github.com/vercel/next.js/tree/canary/examples/custom-serverAdvanced Features: Custom Server | Next.js
Start a Next.js app programmatically using a custom server.
GitHub
next.js/examples/custom-server at canary · vercel/next.js
The React Framework. Contribute to vercel/next.js development by creating an account on GitHub.
After trying to add some functionality to my custom server, I realised:
- Paths I had set up in my tsconfig.json were not recognised
- The .mjs file extension is not supported
The paths fix was easy. All I did was:
1. Install the following dependency:
2. Add the following to my tsconfig.server.json file:
Now, for the .mjs file, I really wanted to keep the .mjs file extension, but I just couldn't figure out a way to get it working. In the end, I reluctantly converted
src/env.mjs → src/env.js
.
If anyone figures out how to get it working, I would love to hear it!Seems like the .mjs file is really throwing everyone off with the custom server. Since you had to re-write is as a .js file, did you have to use require() in the env.js file?
I did not, but I just saw that the build failed. Looks like I have a bit more tinkering to do to get this working…
Here is my current repo, I thought I had everything figure it out but it appears that none of my tRPC routes are working in production: https://github.com/stuible/t3-2-poc
GitHub
GitHub - stuible/t3-2-poc: Proof of concept T3 App
Proof of concept T3 App. Contribute to stuible/t3-2-poc development by creating an account on GitHub.
Keep in mind I require a custom server but I need websocket support
I see, so you have moved the env import outside of you next config. That would fix the problem I am running into right now!
Ya that appeared to be the only way to get it to compile, but I believe it also stops .env validation from happening during development
Hey, curious if y'all got this to work?
I'm also looking for a solution to this problem.
As far as I know, this is still an ongoing problem. It seems the consensus is the custom server will work, but only without the environment variable validation.
So only solution is to host the wss server as an separate application?
How do I remove the environment variable validation?
I would have to defer to either @moloch or @james .
@asmodig remove the env import from the next config file
@asmodig I had to remove the env import just as james describes