Next.js Discord

Discord Forum

Next JS bcrypt build error (one project builds, one doesn't)

Answered
Chausie posted this in #help-forum
Open in Discord
ChausieOP
I am a newbie in Next JS (came from C# ASP .NET Core trying to learn a new stack), and I've bumped into such a problem, I have a project called nextjs-dashboard the one you learn on the Next JS learning course, and I also have a project that I have built from zero (using create-next-app). Thing is I use the same code and same packages as the nextjs-dashboard project but I get such errors (see screenshots, regarding bcrypt as far as I see). I am also sharing code and package.json from both projects in the screenshots.
Answered by Chausie
Really I do not understand, I am thinking that maybe the middleware file runs in Edge runtime, and that I am passing it a next auth instance that contains a package it cannot use... maybe
View full answer

53 Replies

ChausieOP
I am wondering why it works in one project and in other not.
I know there is a bcryptjs package that works great, but it's really annoying that I cannot understand why it works in one project but in the other not.
Polar bear
using bcryptjs instead of bcrypt should fix the problem
@Chausie I know there is a bcryptjs package that works great, but it's really annoying that I cannot understand why it works in one project but in the other not.
Polar bear
I've had the same problem too when trying to build my project using esbuild. I think its because it has some files in the package that can't be bundled. in yours and my case it's that nw-pre-gyp/index.html
ChausieOP
Can i turn off esbuild? Or how it works
@Chausie Can i turn off esbuild? Or how it works
Polar bear
nextjs doesn't use esbuild
But I tried to bundle the bcrypt package but I just can't. So I gave up and use bcryptjs instead
@Polar bear nextjs doesn't use esbuild
ChausieOP
Anyways, okay, thank you
Polar bear
@Chausie wait I just saw that you successfully build it
which one works?
ChausieOP
nextjs-dashboard works
The project that you build during the next js course
Polar bear
Can I see your next.config file?
both projects
ChausieOP
Yeah, wait a second
https://github.com/vladimirmazarakis/nextjs-dashboard

Btw here you can see the nextjs-dashboard project,
@Chausie https://github.com/vladimirmazarakis/nextjs-dashboard Btw here you can see the nextjs-dashboard project,
ChausieOP
The other project file is same but the file extension is mjs
instead of .js
Sorry
Polar bear
that's weird
I faced the problem because I was trying to build my project into a single standalone file
ChausieOP
If you check the learn Next js course in the encryption lesson I think its called like that simpe bcrypt is being used, this course is on the official website
Polar bear
yeah you're right it can build
Polar bear
@Chausie Can you try creating scripts/seed.js and import bcrypt to the file just like in the dashboard project ?
@Polar bear <@703629960073707560> Can you try creating scripts/seed.js and import bcrypt to the file just like in the dashboard project ?
ChausieOP
I can, but I think it's pointless, as bcrypt in the dashboard project is also used in auth.ts file at the root of the project and works well something that does not happen on my other project
I am more than sure, that there is some sort of setting/configuration that has to do with this difference, but I can not figure out what on myself
Polar bear
You should try it. Since the lesson says that bcrypt requires a separate file which the dashboard project has but your project doesn't.
ChausieOP
Hmm... okay, let's try
Nah, same error
Polar bear
tried it and same error. This is weird asf. you're right it may be some configs that we don't know
Polar bear
I followed the lesson where it used bcrypt with next-auth and I can build/run my app just fine
I think if you set up next-auth right it should work with no extra configs needed
@Chausie continue here, I’ll delete the duplicate
@Polar bear I think if you set up next-auth right it should work with no extra configs needed
ChausieOP
I did setup it right, literally the same way I would say.
ChausieOP
Sooooo, I found the solution...

with such a middleware file everything works
But if you do it like that:
Everything breaks, I will show you the code of auth.ts and auth.config:
I cannot understand how it affects it though
@Chausie Sooooo, I found the solution... with such a middleware file everything works
Polar bear
So it was the middleware?
ChausieOP
Yeah, everything works, now, I think it is somehow related to the runtime thing
Polar bear
I don't understand either. tbh I wouldn't know you have to wrap the config with NextAuth again in the middleware.
this lib seems messed up
ChausieOP
Really I do not understand, I am thinking that maybe the middleware file runs in Edge runtime, and that I am passing it a next auth instance that contains a package it cannot use... maybe
Answer
ChausieOP
Thats like my personal theory
Anyways middleware does not need the instance created in auth.ts, it only needs the auth function which is implemented in auth.config
Polar bear
a bit tricky I see
Anyways congrats man
ChausieOP
Thank you
Spotted Rail
@Chausie I had the same issue, I had followed the guide and built the nextjs dashboard, it was working fine. But when I tried the same thing on one of my personal projects i face the issue u have mentioned
So the problem is with either nextjs or next-auth i believe
@Spotted Rail So the problem is with either nextjs or next-auth i believe
ChausieOP
No, the problem is not in next nor in next-auth as far as I think, it's more about understanding how they work