Search code examples
expressbcryptbun

Bun 1.0.0 Bun.password.hash ... error cannot find name Bun


I am migrating an express project to use Bun. While using bcrypt I get this error error: Cannot find module "/home/ab/Documents/projects/freeaitools/server/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node" from "/home/ab/Documents/projects/freeaitools/server/node_modules/bcrypt/bcrypt.js" pure virtual method called when I use bun run. I want to use their build in bcrypt hashing. This is what i have

const hashedPass = await Bun.password.hash(password,{
                algorithm:"bcrypt",
                cost:10
           });

This gives me this error Cannot find name 'Bun'. I know there is a global Bun object but my code can't access it. Is there something I need to do so I can access it?

I have checked and there is an issue with Bun and bcrypt npm package. So I now I am trying to get Bun.password.hash to work


Solution

  • If you are using TypeScript, make sure sure you have a valid tsconfig.json and you have the bun types (https://bun.sh/docs/typescript) inside of your tsconfig.json and package.json under the dev dependencies. Also make sure you have a valid package.json of course. Both of these can be achieved by running bun init.

    Then try deleting your node_modules folder running bun install again. For me on bun v1.0.11 your example completely works without any other configuration.