Next.js Discord

Discord Forum

fs module missing using bcrypt

Unanswered
<Milind ツ /> posted this in #help-forum
Open in Discord
Avatar
<Milind ツ />OP
Hi, I have been trying to use bcrypt in my nextjs project that uses the pages router. On installing bcrypt and calling the bcrypt.hash() to create a hash password, it throws me

Import trace for requested module:
./node_modules/@mapbox/node-pre-gyp/lib/ sync ^\.\/.*$
./node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js
./node_modules/bcrypt/bcrypt.js
./pages/todoify/resetPass.tsx
- wait compiling...
- error ./node_modules/@mapbox/node-pre-gyp/lib/clean.js:8:0
Module not found: Can't resolve 'fs'

https://nextjs.org/docs/messages/module-not-found


sample code:
const salt = bcrypt.genSaltSync(10);
const hashedPass = bcrypt.hashSync(pass, salt);


however, if i remove these lines of code and refresh the web page, page loads fine. I do not have fs installed in dependency but on installing fs, it makes no difference.

19 Replies

Avatar
<Milind ツ />OP
things i have tried till now:

- updated node to latest version
- restarted windows
- removed node_modules and .next folder and did npm i
Avatar
Northeast Congo Lion
any updates?
Avatar
<Milind ツ />OP
Oh yea, so turns out bcrypt does not work on the browser. We have to either use bcryptjs or use bcrypt on server/api routes
Tried the api route way and it worked
Tho i came across another issue later on.

I am working on a flutter app that uses realmdb so for login purposes, when we use bcrypt on flutter side as well to hash the password, it always generates random hashes.

Any trick you know for this?
Avatar
ncls.
Why do you want to hash the password client-side?
Avatar
<Milind ツ />OP
I know I shouldn't but I am not a backend developer. Plus I can't seem to be able to configure how the login methods works on realm app services
Avatar
ncls.
It's not that you shouldn't, but I don't know why you would. It's usually the server's job.
Idk Realm but from what I can see, it expects the password as plain text, doesn't it?
Avatar
<Milind ツ />OP
Yes, the default mechanism of email/pass provider they offer used plain email and passwords.

They do offer an option to create a custom function that handles the user creation.
But that doesn't work properly on flutter SDK.
Avatar
ncls.
And which one are you using?
Avatar
<Milind ツ />OP
The default mechanism
App isn't live yet so plain passwords it is
Avatar
ncls.
Realm most likely hashes the passwords themselves. If they wouldn't they would be dumb so don't worry.
Avatar
<Milind ツ />OP
Would still need to confirm they do or not
Avatar
ncls.
I would bet both of my hands that they do. If they wouldn't do it, they would risk a big and unnecessary law suit which they could have prevented with 2 additional lines of code.
Every company does and big companies that offer authentication services usually know what they are doing and have people in place whose only job it is to check if the data is secure
Avatar
<Milind ツ />OP
Ah, then it's safe to proceed without bcrypt
Avatar
ncls.
Yes